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?
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'?
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
}
//...
};
#include <iostream.h>
using std::cout;
using std::end;
class A{//abstract class
public:
A(){}
virtualvoid 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;
}