C++让基类握住派生类的指针
2007-08-09 23:15
232 查看
转自:http://blog.csdn.net/viewpl/archive/2006/09/24/1270055.aspx
握住派生类指针是指拥有一个派生类指针的成员,暂且不谈这样做干什么,先来看一下如何这样做
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
// sample1.cpp
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <cstdlib>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <iostream>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <string>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using namespace std;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class derived;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class base
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
base()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p = static_cast<derived*>(this);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
~base()...{} // 没用多态,故这里没有用virtual
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
derived * p;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class derived : public base
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
derived()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void sayhi()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << "hi" << endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cram.resize(1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string cram;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int main(int argc, char *argv[])
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
derived d;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
d.p->sayhi();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
system("PAUSE");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return EXIT_SUCCESS;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
.........
关于为什么要用string cram后面用到说明,首先上面的代码不能编译,错误发生在
p = static_cast<derived*>(this); // error invalid static_cast from type `base* const' to type `derived*'
这是因为static_cast在base*和derived*之间作转换时,必须知道这两个类的内存间隔距离(offset),确切的说是要知道derived的定义,而在base的构造函数中,derived的定义显然无法得知。既然文雅的static_cast无法解决问题,那来野蛮一点的吧。把上面的base构造函数改为:
base()
{
p = (derived*)(this);
}
这次程序编译通过了,运行也没问题,屏幕上显示因为调用sayhi而出现的hi。为啥哪个sayhi函数要调用一下无聊的cram.resize呢,是为了检测base中得到的p的确是一个有效derived*指针,一定要在sayhi中让this指针派上用场。防止这样的情况发生:
假如sayhi这样定义
void derived::sayhi()
{
cout << "hi" << endl;
}
...
derived * p = 0;
p->sayhi(); // ok,一切正常,因为sayhi中根本没有用到this指针,所以p为0或其他任何值都无所谓了
回过头来讨论上面那个C语法的强制转换,把编译这一关强行闯过去了。可俗话说“逃得了和尚逃不了庙”,如果情况稍微发生一点变化,运行就会出错,假如把derived改成这样:
class derived : public string, public base
{
public:
derived()
{
}
void sayhi()
{
cout << "hi" << endl;
cram.resize(1);
}
string cram;
};
程序运行到sayhi就会报错(cram.resize终于派上用场了,哈哈),只是加了一个基类,戏剧性的是把public string 和public base调换一下次序程序又正常了,以此可以猜到C++是怎么安排对象在内存中的布局了,最上层最前面的基类安排在最终对象的最前端,如果base不放在最前端,那么base的this指针就不能强制转换为derived*。多继承在实践当中经常出现,程序员一般不会也不想考虑同一级基类的先后继承次序,加了一个string基类竟然会导致风马牛不相及的base类无法工作,我想任何程序员都惧怕这种神经质式的错误发生。
既然这个方法不行,还有另外一个方法,可以花点力气改造一下base的构造函数,在构造base的时候把derived指针传入:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
// sample2.cpp
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <cstdlib>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <iostream>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <string>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using namespace std;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class derived;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class base
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
base(derived * par)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
: p(par)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
~base()...{}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
derived * p;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class derived : public base
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
derived() : base(this) // 这里把derived指针传给了base
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void sayhi()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << "hi" << endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cram.resize(1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string cram;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int main(int argc, char *argv[])
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
derived d;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
d.p->sayhi();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
system("PAUSE");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return EXIT_SUCCESS;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
.........
虽然上面的代码可以工作,而且不存在潜在问题,然而我对这个办法仍然感到不满意,假如有n多个base的派生类,难道要写n多种base的构造函数吗?而且写派生类的程序员一定要记得调用一下基类的构造函数,这样不仅增加了代码量,而且增加了出错的可能性。
难道没有更好的办法实现这个技术吗?答案是有的,使用模版。
使用模版是为了让原先的static_cast能够工作,这样base::p就不会一不小心指在一个错误的地方了。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
// sample3.cpp
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <cstdlib>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <iostream>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <string>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using namespace std;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
template<typename T>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class base
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
base()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p = static_cast<T*>(this);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
~base()...{}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
T * p;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class derived : public base<derived>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
derived()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void sayhi()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << "hi" << endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cram.resize(2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string cram;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int main(int argc, char *argv[])
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
derived d;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
d.p->sayhi();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
system("PAUSE");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return EXIT_SUCCESS;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
.........
哇,连原来的class derived;前置声明也省了,这下把base放在继承次序的任何位置都没有问题了!class derived : public string, public base<derived>
与sample1不同的是,这一次编译器先处理的是derived,当遇到base<derived>的时候,再去产生一个base<derived>类的实体(instantiation),然后再编译这个实体,而这个时候编译器已经恰好可以算出derived和base<derived>之间的offset,因为它知道base<derived>在derived中所处的位置,于是static_cast可以正确工作了。而且代码量保持最小,派生类也不需要为了实现这个功能而“提供额外帮助”。
至此可以说比较完美得实现了基类握住派生类指针的功能,但这样做有什么用途呢?容易想到的用途是接口和实现的分离然后灵活装配。
假如我们要实现描绘三角形(triangle),圆形(circle),方形(rectangle),而且用户需要在这三种形状内部里面嵌入一个任意这三种形状的一种,也就是3*3,9种形状,我没有办法给它们命名,只好这样称呼它们: triangle-triangle,circle-circle,rectangle-rectangle,triangle-circle......简便起见,使用首字母缩写为tt,cc,rr,tc,tr,cr,ct,rt,rc(约定在前面的字母所代表的形状画在外围,后面的代表相对应的“内围”)。
如何设计这个类型体系呢?假如使用传统C++模式,定义九个这样的class让他们都继承shape这样的基类,然后使用虚函数多态,肯定可以实现这些功能,但是这样做势必造成类里面的实现代码有大量重复部分。于是我们把画整个形状分解为两步,画外围,和画内围。画外围定义三种实现类,画内围定义三种实现类,我们的最终类去分别继承其中两个就获得了一个用户所要的新形状,目的就是为了避免重复代码。
以实现rc为例,先构建类型框架:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class outside_rectangle_impl // 实现描绘外围图形部分
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void draw_outside_impl()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class inside_circle_impl // 实现描绘内维图形部分
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void draw_inside_impl()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class rc :
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public outside_rectangle_impl,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public inside_circle_impl
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void draw()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
draw_outside_impl();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
draw_inside_imp();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
long left, top;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
long right, bottom;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};.........
问题来了,要实现draw_outside_impl,draw_inside_impl,必须知道形状的相关属性(顶点坐标位置),然而相关属性在派生类中(至于为什么要把属性数据放在派生类中不是本文的话题范围), 这个时候,让基类握住派生类指针技术就可以派上用场了。完整代码如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
// sample4.cpp
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <cstdlib>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <iostream>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <string>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using namespace std;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
template<typename T>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class shape_impl
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
shape_impl() :
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p(static_cast<T*>(this))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void draw()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p->draw_inside_impl();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p->draw_outside_impl();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
T * p;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
template<typename T>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class outside_rectangle_impl
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
outside_rectangle_impl() :
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p(static_cast<T*>(this))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void draw_outside_impl()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << "draw_outside_impl with left "
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
<< p->left << " top "
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
<< p->top << " right "
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
<< p->right << " bottom "
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
<< p->bottom << endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
T * p;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
template<typename T>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class inside_circle_impl
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
inside_circle_impl() :
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p(static_cast<T*>(this))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void draw_inside_impl()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << "draw_inside_impl with left "
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
<< p->left << " top "
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
<< p->top << " right "
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
<< p->right << " bottom "
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
<< p->bottom << endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
T * p;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class rc :
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public shape_impl<rc>,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public outside_rectangle_impl<rc>,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public inside_circle_impl<rc>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
friend class outside_rectangle_impl<rc>;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
friend class inside_circle_impl<rc>;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
rc()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
left = top = 100;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
right = bottom = 200;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void draw() // 重写这个函数,如果要实现自定义的描绘
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
shape_impl<rc>::draw();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
long left, top;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
long right, bottom;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int main(int argc, char *argv[])
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
rc obj;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
obj.draw();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
system("PAUSE");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return EXIT_SUCCESS;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
...............
更完善地,这个例子还定义了一个shape_impl,使得最终的派生类连draw的函数实体都不必写就立即得到了一个用户想要的类。
至此这个话题接近尾声了,最后补充一个问题:在钻石型多继承中,如何使最顶端类握住最终派生类。
钻石型多继承,是C++中一个著名的“难题”,在实际C++项目中程序员一般都是尽量避免形成这样的结构,也就是这样:
class base
{
};
class middle1 : public base
{
};
class middle2 : public base
{
};
class derived :
public middle1,
public middle2
{
};
在这样的类型结构中,derived包含了两份base实体,如果我们依旧使用原先的方法:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
template<typename T>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class base
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
base()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
m_derived = static_cast<T*>(this);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
T * m_derived;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
template<typename T>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class middle1 : public base<T>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
template<typename T>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class middle2 : public base<T>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class derived :
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public middle1<derived>,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public middle2<derived>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
............
会得到一个类似这样的编译错误,`base<derived>' is an ambiguous base of `derived'
因为现在把base转换derived有两条路径可以选,通过middle1或middle2,编译器不知道选择哪一条,于是歧异产生了。幸运的是利用模版,还是可以轻松解决这个问题。像这样:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
// sample5.cpp
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <cstdlib>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <iostream>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <string>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using namespace std;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
template<typename T, typename M>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class base
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
base()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
m_derived = static_cast<T*>(static_cast<M*>(this));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
T * m_derived;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
template<typename T>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class middle1 : public base<T, middle1<T> >
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string str;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
template<typename T>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class middle2 : public base<T, middle2<T> >
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bool bo;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class derived :
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public middle1<derived>,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public middle2<derived>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
typedef middle1<derived> middle1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
typedef middle2<derived> middle2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void sayhi()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << "hi" << endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cram.resize(1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string cram;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int main(int argc, char *argv[])
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
derived d;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
assert(d.middle1::m_derived == d.middle2::m_derived); // 两个指针肯定指在同一个地方,于是下面两个调用都没问题
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
d.middle1::m_derived->sayhi();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
d.middle2::m_derived->sayhi();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
system("PAUSE");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return EXIT_SUCCESS;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
...............
经过两个static_cast告诉了编译器每个base的转换路径,使得两份base的m_derived成员都指在了正确的地方。
握住派生类指针是指拥有一个派生类指针的成员,暂且不谈这样做干什么,先来看一下如何这样做
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
// sample1.cpp
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <cstdlib>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <iostream>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <string>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using namespace std;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class derived;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class base
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
base()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p = static_cast<derived*>(this);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
~base()...{} // 没用多态,故这里没有用virtual
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
derived * p;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class derived : public base
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
derived()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void sayhi()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << "hi" << endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cram.resize(1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string cram;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int main(int argc, char *argv[])
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
derived d;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
d.p->sayhi();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
system("PAUSE");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return EXIT_SUCCESS;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
.........
关于为什么要用string cram后面用到说明,首先上面的代码不能编译,错误发生在
p = static_cast<derived*>(this); // error invalid static_cast from type `base* const' to type `derived*'
这是因为static_cast在base*和derived*之间作转换时,必须知道这两个类的内存间隔距离(offset),确切的说是要知道derived的定义,而在base的构造函数中,derived的定义显然无法得知。既然文雅的static_cast无法解决问题,那来野蛮一点的吧。把上面的base构造函数改为:
base()
{
p = (derived*)(this);
}
这次程序编译通过了,运行也没问题,屏幕上显示因为调用sayhi而出现的hi。为啥哪个sayhi函数要调用一下无聊的cram.resize呢,是为了检测base中得到的p的确是一个有效derived*指针,一定要在sayhi中让this指针派上用场。防止这样的情况发生:
假如sayhi这样定义
void derived::sayhi()
{
cout << "hi" << endl;
}
...
derived * p = 0;
p->sayhi(); // ok,一切正常,因为sayhi中根本没有用到this指针,所以p为0或其他任何值都无所谓了
回过头来讨论上面那个C语法的强制转换,把编译这一关强行闯过去了。可俗话说“逃得了和尚逃不了庙”,如果情况稍微发生一点变化,运行就会出错,假如把derived改成这样:
class derived : public string, public base
{
public:
derived()
{
}
void sayhi()
{
cout << "hi" << endl;
cram.resize(1);
}
string cram;
};
程序运行到sayhi就会报错(cram.resize终于派上用场了,哈哈),只是加了一个基类,戏剧性的是把public string 和public base调换一下次序程序又正常了,以此可以猜到C++是怎么安排对象在内存中的布局了,最上层最前面的基类安排在最终对象的最前端,如果base不放在最前端,那么base的this指针就不能强制转换为derived*。多继承在实践当中经常出现,程序员一般不会也不想考虑同一级基类的先后继承次序,加了一个string基类竟然会导致风马牛不相及的base类无法工作,我想任何程序员都惧怕这种神经质式的错误发生。
既然这个方法不行,还有另外一个方法,可以花点力气改造一下base的构造函数,在构造base的时候把derived指针传入:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
// sample2.cpp
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <cstdlib>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <iostream>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <string>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using namespace std;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class derived;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class base
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
base(derived * par)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
: p(par)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
~base()...{}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
derived * p;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class derived : public base
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
derived() : base(this) // 这里把derived指针传给了base
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void sayhi()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << "hi" << endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cram.resize(1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string cram;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int main(int argc, char *argv[])
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
derived d;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
d.p->sayhi();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
system("PAUSE");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return EXIT_SUCCESS;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
.........
虽然上面的代码可以工作,而且不存在潜在问题,然而我对这个办法仍然感到不满意,假如有n多个base的派生类,难道要写n多种base的构造函数吗?而且写派生类的程序员一定要记得调用一下基类的构造函数,这样不仅增加了代码量,而且增加了出错的可能性。
难道没有更好的办法实现这个技术吗?答案是有的,使用模版。
使用模版是为了让原先的static_cast能够工作,这样base::p就不会一不小心指在一个错误的地方了。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
// sample3.cpp
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <cstdlib>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <iostream>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <string>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using namespace std;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
template<typename T>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class base
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
base()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p = static_cast<T*>(this);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
~base()...{}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
T * p;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class derived : public base<derived>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
derived()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void sayhi()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << "hi" << endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cram.resize(2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string cram;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int main(int argc, char *argv[])
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
derived d;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
d.p->sayhi();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
system("PAUSE");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return EXIT_SUCCESS;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
.........
哇,连原来的class derived;前置声明也省了,这下把base放在继承次序的任何位置都没有问题了!class derived : public string, public base<derived>
与sample1不同的是,这一次编译器先处理的是derived,当遇到base<derived>的时候,再去产生一个base<derived>类的实体(instantiation),然后再编译这个实体,而这个时候编译器已经恰好可以算出derived和base<derived>之间的offset,因为它知道base<derived>在derived中所处的位置,于是static_cast可以正确工作了。而且代码量保持最小,派生类也不需要为了实现这个功能而“提供额外帮助”。
至此可以说比较完美得实现了基类握住派生类指针的功能,但这样做有什么用途呢?容易想到的用途是接口和实现的分离然后灵活装配。
假如我们要实现描绘三角形(triangle),圆形(circle),方形(rectangle),而且用户需要在这三种形状内部里面嵌入一个任意这三种形状的一种,也就是3*3,9种形状,我没有办法给它们命名,只好这样称呼它们: triangle-triangle,circle-circle,rectangle-rectangle,triangle-circle......简便起见,使用首字母缩写为tt,cc,rr,tc,tr,cr,ct,rt,rc(约定在前面的字母所代表的形状画在外围,后面的代表相对应的“内围”)。
如何设计这个类型体系呢?假如使用传统C++模式,定义九个这样的class让他们都继承shape这样的基类,然后使用虚函数多态,肯定可以实现这些功能,但是这样做势必造成类里面的实现代码有大量重复部分。于是我们把画整个形状分解为两步,画外围,和画内围。画外围定义三种实现类,画内围定义三种实现类,我们的最终类去分别继承其中两个就获得了一个用户所要的新形状,目的就是为了避免重复代码。
以实现rc为例,先构建类型框架:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class outside_rectangle_impl // 实现描绘外围图形部分
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void draw_outside_impl()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class inside_circle_impl // 实现描绘内维图形部分
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void draw_inside_impl()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class rc :
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public outside_rectangle_impl,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public inside_circle_impl
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void draw()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
draw_outside_impl();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
draw_inside_imp();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
long left, top;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
long right, bottom;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};.........
问题来了,要实现draw_outside_impl,draw_inside_impl,必须知道形状的相关属性(顶点坐标位置),然而相关属性在派生类中(至于为什么要把属性数据放在派生类中不是本文的话题范围), 这个时候,让基类握住派生类指针技术就可以派上用场了。完整代码如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
// sample4.cpp
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <cstdlib>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <iostream>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <string>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using namespace std;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
template<typename T>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class shape_impl
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
shape_impl() :
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p(static_cast<T*>(this))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void draw()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p->draw_inside_impl();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p->draw_outside_impl();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
T * p;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
template<typename T>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class outside_rectangle_impl
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
outside_rectangle_impl() :
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p(static_cast<T*>(this))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void draw_outside_impl()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << "draw_outside_impl with left "
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
<< p->left << " top "
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
<< p->top << " right "
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
<< p->right << " bottom "
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
<< p->bottom << endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
T * p;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
template<typename T>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class inside_circle_impl
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
inside_circle_impl() :
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p(static_cast<T*>(this))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void draw_inside_impl()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << "draw_inside_impl with left "
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
<< p->left << " top "
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
<< p->top << " right "
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
<< p->right << " bottom "
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
<< p->bottom << endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
T * p;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class rc :
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public shape_impl<rc>,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public outside_rectangle_impl<rc>,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public inside_circle_impl<rc>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
friend class outside_rectangle_impl<rc>;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
friend class inside_circle_impl<rc>;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
rc()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
left = top = 100;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
right = bottom = 200;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void draw() // 重写这个函数,如果要实现自定义的描绘
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
shape_impl<rc>::draw();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
long left, top;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
long right, bottom;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int main(int argc, char *argv[])
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
rc obj;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
obj.draw();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
system("PAUSE");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return EXIT_SUCCESS;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
...............
更完善地,这个例子还定义了一个shape_impl,使得最终的派生类连draw的函数实体都不必写就立即得到了一个用户想要的类。
至此这个话题接近尾声了,最后补充一个问题:在钻石型多继承中,如何使最顶端类握住最终派生类。
钻石型多继承,是C++中一个著名的“难题”,在实际C++项目中程序员一般都是尽量避免形成这样的结构,也就是这样:
class base
{
};
class middle1 : public base
{
};
class middle2 : public base
{
};
class derived :
public middle1,
public middle2
{
};
在这样的类型结构中,derived包含了两份base实体,如果我们依旧使用原先的方法:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
template<typename T>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class base
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
base()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
m_derived = static_cast<T*>(this);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
T * m_derived;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
template<typename T>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class middle1 : public base<T>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
template<typename T>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class middle2 : public base<T>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class derived :
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public middle1<derived>,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public middle2<derived>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
............
会得到一个类似这样的编译错误,`base<derived>' is an ambiguous base of `derived'
因为现在把base转换derived有两条路径可以选,通过middle1或middle2,编译器不知道选择哪一条,于是歧异产生了。幸运的是利用模版,还是可以轻松解决这个问题。像这样:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
// sample5.cpp
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <cstdlib>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <iostream>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <string>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using namespace std;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
template<typename T, typename M>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class base
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
base()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
m_derived = static_cast<T*>(static_cast<M*>(this));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
T * m_derived;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
template<typename T>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class middle1 : public base<T, middle1<T> >
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string str;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
template<typename T>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class middle2 : public base<T, middle2<T> >
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bool bo;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class derived :
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public middle1<derived>,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public middle2<derived>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
typedef middle1<derived> middle1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
typedef middle2<derived> middle2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void sayhi()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << "hi" << endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cram.resize(1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string cram;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int main(int argc, char *argv[])
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
derived d;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
assert(d.middle1::m_derived == d.middle2::m_derived); // 两个指针肯定指在同一个地方,于是下面两个调用都没问题
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
d.middle1::m_derived->sayhi();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
d.middle2::m_derived->sayhi();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
system("PAUSE");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return EXIT_SUCCESS;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
...............
经过两个static_cast告诉了编译器每个base的转换路径,使得两份base的m_derived成员都指在了正确的地方。
相关文章推荐
- C++让基类握住派生类的指针
- C++让基类握住派生类的指针
- 基类(base class)和派生类(derived class)指针在C++的相互转化
- C++中,基类的指针指向派生类的对象
- C++继承中基类和派生类之间指针传递问题
- C++中通过派生类调用第二基类的普通成员函数时this指针的调整
- C++的继承操作---基类指针访问派生类问题---基类成员恢复访问属性问题
- 关于C++基类、派生类的引用和指针
- C++动态联编的好处,为什么要用基类的指针指向派生类的方式来实现重写
- C++中指向派生类的基类指针、基类引用的一点总结
- C++基类指针指向的派生类对象内存的释放
- 【C++】基类指针可以指向派生类对象,派生类指针不可以指向基类对象
- C++通过基类指针delete派生类数组,析构函数是虚函数,程序为什么会崩溃? https://www.zhihu.com/question/30838092/answer/49623765
- C++ 基类指针指向派生类对象
- C++ 基类、派生类对象指针的声明与使用
- C++ 派生类指针指向基类对象
- C++基类、派生类指针转换
- C++入门学习:虚析构、构造中不能实现多态、基类指针指向派生类数组的弊端
- 基类握住派生类指针
- C++基础之基类派生类指针间强转问题(基类派生类傻傻分不清)