虛擬函數+動態連結

此做法在VC6中會有 ERROR C2555,不過我有解決方案,有興趣的人可以參考。
#include <iostream>

using namespace std;

class A
{
public:
 virtual A* fun1() = 0;
 virtual A* fun2() = 0;
};

class B : public A
{
public:
 B* fun1() { cout << "B::fun1()" << endl; return this; }
 B* fun2() { cout << "B::fun2()" << endl; return this; }
};

class C : public A
{
public:
 C* fun1() { cout << "C::fun1()" << endl; return this ; }
 C* fun2() { cout << "C::fun2()" << endl; return this ; }
};

int main()
{
B b1;
C c1;

A* a = &b1;
a->fun1()->fun2();

a = &c1;
a->fun1()->fun2();
}

沒有留言:

張貼留言

(什麼是留言欄訊息?)