2 classes, 1 method

Discussion in 'C++' started by earthwormgaz, Jun 27, 2008.

  1. earthwormgaz

    earthwormgaz Guest

    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?
    earthwormgaz, Jun 27, 2008
    #1
    1. Advertising

  2. earthwormgaz

    earthwormgaz Guest

    On 27 Jun, 14:54, Victor Bazarov <> 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 :(
    earthwormgaz, Jun 27, 2008
    #2
    1. Advertising

  3. earthwormgaz

    Joe Greer Guest

    earthwormgaz <> wrote in news:ecab2b90-d045-
    :

    > 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
    Joe Greer, Jun 27, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David

    Classes within classes

    David, Jul 21, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    4,927
    David
    Jul 22, 2005
  2. lonelyplanet999
    Replies:
    1
    Views:
    2,201
    VisionSet
    Nov 13, 2003
  3. Carfield Yim
    Replies:
    1
    Views:
    1,397
    Andrew Thompson
    May 31, 2004
  4. Razvan
    Replies:
    11
    Views:
    895
    Andrew Thompson
    Jul 17, 2004
  5. Kyung won Cheon
    Replies:
    0
    Views:
    187
    Kyung won Cheon
    Nov 21, 2008
Loading...

Share This Page