PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > 2 classes, 1 method
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
2 classes, 1 method

Réponse
 
LinkBack Outils de la discussion
Vieux 27/06/2008, 14h50   #1
earthwormgaz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 2 classes, 1 method

So, I've got a class, A, and it does loads more than I want ..

class A {
doStuffIDontCareAbout() { } // loads of these
methodIDoNeed() { }
};

I've started writing a slim version that comes without all the cruft.
However, I've found there's methodIDoNeed. What's the best way to
share it between the two classes?
  Réponse avec citation
Vieux 27/06/2008, 14h54   #2
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: 2 classes, 1 method

earthwormgaz wrote:
> So, I've got a class, A, and it does loads more than I want ..
>
> class A {
> doStuffIDontCareAbout() { } // loads of these
> methodIDoNeed() { }
> };
>
> I've started writing a slim version that comes without all the cruft.
> However, I've found there's methodIDoNeed. What's the best way to
> share it between the two classes?


"Share"? Yon can write a [relatively] simple wrapper around the 'A' and
provide only the functionality you need:

class MyA {
A a;
public:
void methodIDoNeed() { return a.methodIDoNeed(); }
};

Is that what you call "share"?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
  Réponse avec citation
Vieux 27/06/2008, 15h06   #3
earthwormgaz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: 2 classes, 1 method

On 27 Jun, 14:54, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> "Share"? Yon can write a [relatively] simple wrapper around the 'A' and
> provide only the functionality you need:
>
> class MyA {
> A a;
> public:
> void methodIDoNeed() { return a.methodIDoNeed(); }
> };
>
> Is that what you call "share"?
>
>


Trouble is, A is bloated for my needs and does loads of stuff I don't
want. I just want a way for both classes to be able to use this
method.

I am thinking I could do it by pulling that method out into a function
which A and MyA can both call, but that's not very OO
  Réponse avec citation
Vieux 27/06/2008, 15h26   #4
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: 2 classes, 1 method

earthwormgaz wrote:
> On 27 Jun, 14:54, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
>> "Share"? Yon can write a [relatively] simple wrapper around the 'A' and
>> provide only the functionality you need:
>>
>> class MyA {
>> A a;
>> public:
>> void methodIDoNeed() { return a.methodIDoNeed(); }
>> };
>>
>> Is that what you call "share"?
>>
>>

>
> Trouble is, A is bloated for my needs and does loads of stuff I don't
> want. I just want a way for both classes to be able to use this
> method.
>
> I am thinking I could do it by pulling that method out into a function
> which A and MyA can both call, but that's not very OO


Well, if you want to be "OO", put that functionality and all the
necessary data into a separate class and make your A and MyA derive from
it... Same thing, only wrapped in a class. Better OO? <shrug>

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
  Réponse avec citation
Vieux 27/06/2008, 15h31   #5
Joe Greer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: 2 classes, 1 method

earthwormgaz <earthwormgaz@googlemail.com> wrote in news:ecab2b90-d045-
46eb-aef2-de876d6ee590@z72g2000hsb.googlegroups.com:

> So, I've got a class, A, and it does loads more than I want ..
>
> class A {
> doStuffIDontCareAbout() { } // loads of these
> methodIDoNeed() { }
> };
>
> I've started writing a slim version that comes without all the cruft.
> However, I've found there's methodIDoNeed. What's the best way to
> share it between the two classes?


You can always refactor it (and the data it needs) into a base class and
inherit that from both (privately if it's an implementation detail
only).

class base {
public:
methodIDoNeed(){}
private:
// data needed by methodIDoNeed()
};

class A : private /* or public */ base {
doStuffIDon'tCareAbout(){}
};

class SlimA : private base {
};


Or, if SlimA is a true subset of A, then just move stuff to SlimA, and
inherit SlimA from A.


class SlimA {
public:
methodIDoNeed(){}
private:
// data needed by methodIDoNeed()
};

class A : private /* or public */ SlimA {
doStuffIDon'tCareAbout(){}
};


That will get you code sharing anyway. Be careful with these kinds of
refactorings though. If you aren't careful to keep thing logically
meaningful, you can end up with a spaghetti-like mess and not be able to
make heads or tails of anything.

If you just want a smaller interface to the existing A, then you define
an abstract base class with the interface you want, inherit that
publicly in A and make your methods etc use the interface instead of A.

class ISlimA {
public:
virtual ~ISlimA(){}
virtual methodIDoNeed() = 0;
};

class A : public ISlimA {
doStuffIDontCareAbout() { } // loads of these
methodIDoNeed() { }
};

A a;

ISlimA & sa = a;
..
..
..

I didn't actually compile any of the above, but I think it's right.

HTH,
joe

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 17h41.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13765 seconds with 13 queries