Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > 2 classes, 1 method

Reply
Thread Tools

2 classes, 1 method

 
 
earthwormgaz
Guest
Posts: n/a
 
      06-27-2008
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?
 
Reply With Quote
 
 
 
 
earthwormgaz
Guest
Posts: n/a
 
      06-27-2008
On 27 Jun, 14:54, Victor Bazarov <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Joe Greer
Guest
Posts: n/a
 
      06-27-2008
earthwormgaz <(E-Mail Removed)> wrote in news:ecab2b90-d045-
http://www.velocityreviews.com/forums/(E-Mail Removed):

> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
method def in method vs method def in block Kyung won Cheon Ruby 0 11-21-2008 08:48 AM
invoke a method by reflection£¨the method's parameters can not be ArrayList? jerry051 ASP .Net 2 08-02-2005 10:35 AM
BC30289: Statement cannot appear within a method body. End of method assumed. Carlos Oliveira ASP .Net 0 08-19-2004 07:51 PM
Difference between Delete method and RemoveRow method CW ASP .Net 0 04-01-2004 01:07 AM
ASP.NET: BC30289: Statement cannot appear within a method body. End of method assumed. Mike Wilmot ASP .Net 0 12-15-2003 07:49 PM



Advertisments