C++ class design - a class to inherit different functionality

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Harinezumi
    New Member
    • Feb 2008
    • 32

    C++ class design - a class to inherit different functionality

    Hello,

    I have a base class Base with given member variables, getters and setters, but it doesn't do much on its own. Then there are functionalities (Func) which can use this base class's member functions to achieve some complex computation.
    I would like to design this class structure in a way that a derived of Base, Derived can have added functionality with the use of Funcs, without further programming.

    Here's what I thought would work:
    Code:
    class Base {
    public:
       Base(int par1, int par2) : par1(par1), par2(par2) {}
       virtual void GetPar1() { return par1; }
       virtual void GetPar2() { return par2; }
    private:
       int par1, par2;
    };
    
    class FunctionalityBase {
    public:
       FunctionalityBase(B* base) : basePointer(base) {}
    protected:
       Base* basePointer;
    };
    
    class AddedFunctionality1 : public FunctionalityBase {
    public:
       AddedFunctionality1(Base* base) : FunctionalityBase(base) {}
       void AFunctionality() { /*do something through the base pointer*/}
    };
    
    class Derived : public Base, public AddedFunctionality1
    {
    public:
       Derived(int par1, int par2) : Base(par1, par2), AddedFunctionality(this) {}
    };
    This compiles, and does what I want most of the time. However, is there a way to override a member function of Base (for example for logging purposes) in an added functionality class G, derived of FunctionalityBa se?
    Is this a good design? Are there any (better) alternatives?

    Thanks in advance,
    Harinezumi
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    This article pertains exactly to your question. I hope you read it.

    http://bytes.com/forum/thread793836.html.

    Comment

    • Harinezumi
      New Member
      • Feb 2008
      • 32

      #3
      Originally posted by weaknessforcats
      This article pertains exactly to your question. I hope you read it.

      http://bytes.com/forum/thread793836.html.
      Thanks, that's a great article! It's a shame that textbooks still use public virtual functions...

      Comment

      • Harinezumi
        New Member
        • Feb 2008
        • 32

        #4
        The way of separating the public interface from the implementation in derived classes in the article above is great.
        However, when I started using it, a question emerged: what about multiple levels of inheritance, where the most derived class wants to call function in a middle level class?
        E.g.:

        [CODE=cpp]class A
        {
        public:
        A() {}
        void AMethod() { AMethodHook(); }
        private:
        virtual void AMethodHook() {}
        };

        class B : public virtual A
        {
        public:
        B() : A() {}
        private:
        virtual void AMethodHook() {}
        };

        class C : public virtual A
        {
        public:
        C() : A() {}
        private:
        virtual void AMethodHook() {}
        };

        class D : public B, public C
        {
        public:
        D() : B(), C() {}
        private:
        virtual void AMethodHook()
        {
        B::AMethodHook( );
        C::AMethodHook( );
        }
        };
        [/CODE]
        The compiler complains (rightfully) that B::AMethodHook( ) and C::AMethodHook are private within the context.
        Of course, making the AMethodHooks in class B and C protected solves this, but that's not the point.
        So is there a correct solution for this?

        Thanks,
        Harinezumi

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Originally posted by Harinezumi
          class D : public B, public C
          {
          public:
          D() : B(), C() {}
          private:
          virtual void AMethodHook()
          {
          B::AMethodHook( );
          C::AMethodHook( );
          }
          };
          D can never be call private functions in B.

          D overrides the private method in B but it is a B object that calls the B private method.

          Please note the B object must really be a D object so the VTBL pointer contains the address of D::AMethodHook( ).

          Comment

          • Harinezumi
            New Member
            • Feb 2008
            • 32

            #6
            Originally posted by weaknessforcats
            D can never be call private functions in B.

            D overrides the private method in B but it is a B object that calls the B private method.

            Please note the B object must really be a D object so the VTBL pointer contains the address of D::AMethodHook( ).
            Yes, I understand these.
            My question is that is there a way to use the "public interface - private virtual hook method" pattern in a multi-level inheritance hierarchy?
            (btw, is there a name for it?)

            Comment

            Working...