#include <iostream>
using namespace std;
class Base
{
public:
virtual void function1() {cout<<"Base :: function1()\n";};
virtual ~Base(){};
};
class D1: public Base
{
public:
~D1(){};
void function1() { cout<<"D1 :: function1()\n";};
};
class D2: public D1
{
public:
~D2(){};
void function1() { cout<<"D2 :: function1()\n";};
};
int main()
{
D2 *d2=new D2;
D1 *d1=new D1;
Base *b=new Base;
D1 *dd;
dd=d2;
dd->function1();
dd=d1;
dd->function1();
dd=b;
dd->function1();
return (0);
}
小弟先定義class Base->繼承給D1->繼承給D2 then 宣告D1 *dd 指標給各class
當dd指標Base(dd=b)時 compiler 便會出現
error: invalid conversion from 'Base*' to 'D1*' [-fpermissive
原來繼承不可指標逆回上一層