class CBase
{
public:
virtual void fn()
{
;
}
virtual void Add(CBase b)
{
}
virtual void Add(CBase pb)
{
}
};
class CPart :public CBase
{
public:
void fn()
{
cout<<"p:fn"<<endl;
}
};
class CCom:public CBase
{
public:
void fn()
{
cout<< "com:fn"<<endl;
vector<CBase>::iterator itb,ite;
itb = this->m_vb.begin();
ite = this->m_vb.end(); //
for( ; itb != ite ; ++itb)
(*itb)->(*itb).fn();
}
void Add(CBase b)
{
m_vb.push_back( b);
}
protected:
vector<CBase> m_vb; //
};
int main(int argc, char* argv[])
{
CPart p1,p2,p3,p4;
CCom c1,c2;
CBase *pb = &p1;
pb->fn();
pb= &c1;
pb->Add( p1);
pb->Add( p2);
pb->Add( p3);
}
输出:
p:fn
com:fn
//在把对象加入向量时发生了拷贝构造函数的调用。
由于输出的结果如是,可以断定,A默认的拷贝构造函数 对 类对象中用于指向vtable指针的这块空间(1) 的处理
方式是把 A的vtable指针赋给 这块空间(1)。
而不是把p1的指向vtable指针的这块空间得内容(其实是CPart的vtable的指针)赋给 这块空间(1)
//(1)表示这个短语指明的对象相同