Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Member
 
Join Date: Feb 2008
Location: Budapest, Hungary
Posts: 32
#1: Apr 16 '08
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:
Expand|Select|Wrap|Line Numbers
  1. class Base {
  2. public:
  3.    Base(int par1, int par2) : par1(par1), par2(par2) {}
  4.    virtual void GetPar1() { return par1; }
  5.    virtual void GetPar2() { return par2; }
  6. private:
  7.    int par1, par2;
  8. };
  9.  
  10. class FunctionalityBase {
  11. public:
  12.    FunctionalityBase(B* base) : basePointer(base) {}
  13. protected:
  14.    Base* basePointer;
  15. };
  16.  
  17. class AddedFunctionality1 : public FunctionalityBase {
  18. public:
  19.    AddedFunctionality1(Base* base) : FunctionalityBase(base) {}
  20.    void AFunctionality() { /*do something through the base pointer*/}
  21. };
  22.  
  23. class Derived : public Base, public AddedFunctionality1
  24. {
  25. public:
  26.    Derived(int par1, int par2) : Base(par1, par2), AddedFunctionality(this) {}
  27. };
  28.  
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 FunctionalityBase?
Is this a good design? Are there any (better) alternatives?

Thanks in advance,
Harinezumi

Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,366
#2: Apr 16 '08

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


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

http://bytes.com/forum/thread793836.html.
Member
 
Join Date: Feb 2008
Location: Budapest, Hungary
Posts: 32
#3: Apr 21 '08

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


Quote:

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...
Member
 
Join Date: Feb 2008
Location: Budapest, Hungary
Posts: 32
#4: May 9 '08

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


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.:

Expand|Select|Wrap|Line Numbers
  1. class A
  2. {
  3. public:
  4.     A() {}
  5.     void AMethod() { AMethodHook(); }
  6. private:
  7.     virtual void AMethodHook() {}
  8. };
  9.  
  10. class B : public virtual A
  11. {
  12. public:
  13.     B() : A() {}
  14. private:
  15.     virtual void AMethodHook() {}
  16. };
  17.  
  18. class C : public virtual A
  19. {
  20. public:
  21.     C() : A() {}
  22. private:
  23.     virtual void AMethodHook() {}
  24. };
  25.  
  26. class D : public B, public C
  27. {
  28. public:
  29.     D() : B(), C() {}
  30. private:
  31.     virtual void AMethodHook()
  32.     {
  33.         B::AMethodHook(); 
  34.         C::AMethodHook();
  35.     }
  36. };
  37.  
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
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,366
#5: May 9 '08

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


Quote:

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().
Member
 
Join Date: Feb 2008
Location: Budapest, Hungary
Posts: 32
#6: May 13 '08

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


Quote:

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?)
Reply