A Class design problem, need help.

I got this problem when i was designing a class.
This class C has two virtual member functions: A and B.
And each function has two possible implementations: A-Imp1, A-Imp2, B-Imp1 and B-Imp2.

Now, how can I get sub classes of all four combinations?
C1{A-Imp1, B-Imp1}
C2{A-Imp1, B-Imp2}
C3{A-Imp2, B-Imp1}
C4{A-Imp2, B-Imp2}

I don't think it can be achieved through inheritance, or am I wrong?

please give some advices, many thanks.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

struct C
{
    virtual ~C() {}
    virtual void A() const = 0 ;
    virtual void B() const = 0 ;
};

struct A1 : virtual C { virtual void A() const override { std::cout << "A-Imp1\t" ; } } ;
struct A2 : virtual C { virtual void A() const override { std::cout << "A-Imp2\t" ; } } ;
struct B1 : virtual C { virtual void B() const override { std::cout << "B-Imp1\n" ; } } ;
struct B2 : virtual C { virtual void B() const override { std::cout << "B-Imp2\n" ; } } ;

template < typename A_IMPL, typename B_IMPL >
struct D : virtual C, A_IMPL, B_IMPL {} ; // inherits A() and B() via dominance

int main()
{
    C* objects[] = { new D<A1,B1>, new D<A2,B1>, new D<A1,B2>, new D<A2,B2> } ;
    for( C*& ptr : objects ) { ptr->A() ; ptr->B() ; delete ptr ; ptr = nullptr ; }
}
JLBorges, your method is amazing! Thanks a lot!

It will take me sometime to study this trick.
multiple inheritance are usually a good sign of bad design.

Better think of a solution that doesn't need those tricks.
Please avoid multiple inheritance!!!
Thanks for the warning, guys.
I do aware that multi-inheritance is not neat, however it seems to be the only way to maximize code reusing in my current task.
Anyway, I will use this trick first, and keep looking for an alternative solution.
And thanks again, you guys are really helpful.
Cheers,
> multiple inheritance are usually a good sign of bad design.

Yes. When used by people who do not understand it.


> Please avoid multiple inheritance!!!

Right. Do not, repeat Do Not, use the iostream library.
Or forward, bidirectional and random access iterators for that matter.
And completely avoid user-defined exceptions.

Ah... Loki? For heavens sake! Policy-based design, indeed! How dare this Alexandrescu write a book called 'Modern C++ Design'?

I could go on.
1
2
3
4
5
6
7
8
9
10
11
12
13
class c{
private:
   a_imp *foo;
   b_imp *bar;
public:
   void a(){ //note that this is not virtual
      foo->a(); //virtual method in a_imp
   }
   void b(){
      bar->b(); //virtual method in b_imp
   }
//...
};
Runtime resolution.
Taken from my post in a different thread. Multiple inheritance gone wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream.h>
using std::cout;
using std::end;

class A{//abstract class
public:
A(){}
 virtual void doSomething()=0;
//edit: a case where you have data types inherited
/*
 protected:
   std::string name;
   uint_t id;
   
*/
};

class B : public A //implements doSomething
{
public:
B(){}
   void doSomething(){
    cout<< "I do something" << endl;
}
};

class C: public A
{
public:
C(){}
void doSomething(){
  cout<<"I do something else" << endl;
}
};

class D: public B, public C //oops multiple inheritance which do something does the          //compiler call?
{
 public:
D(){}
~D(){}
  //oops houston we have a problem
};

int main()
{
   A* a = new D();

  if( a == 0 ) return -1;

   a->doSomething();//oh no

  delete((D*) a);//edit forgot to cast back in delete operation

  a = 0;

  return 0;
}
Last edited on
Alexandrescu's 'Modern C++ Design' ? I must get one.
Topic archived. No new replies allowed.