I have the following situation
class B {
public:
B() {};
virtual ~B() {};
virtual void seti( int x ) { i = x; };
virtual void setj( int x ) { j = x; };
virtual void add() =0;
protected:
int i;
int j;
};
class D : public B {
public:
virtual void add() { cout << "D-add:" << i + j << endl; };
};
class E: public B {
public:
void seti( int x ) { i = x; };
void add() { cout << "E-add:" << i + j << endl; };
void mult() { cout << "E-mult:" << i * j << endl; };
};
int _tmain(int argc, _TCHAR* argv[])
{
D *d = new D();
d->seti(4); d->setj(5); d->add();
E*e = d;
e->seti(8); e->add(); e->mult();
return 0;
}
I get the following error 1>.\CallBack.cpp(38) : error C2440: 'initializing' : cannot convert from 'D *' to 'E *' 1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-styl`enter code here`e cast
What i want to do is when I instantiate E, I was use all the information / members of D and do some thing more with it. Should I use hierarchical inheritance like above or should I use multi-level inheritance or is there any other better way. Please advise. Thank you !