虛擬函數+動態連結

  1. 虛擬函數+動態連結

虛擬函數+動態連結

原文連結: https://darkblack01.blogspot.com/2012/09/blog-post.html
移植時的最後更新日期: 2015-12-23T14:16:57.734+08:00

此做法在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();
}