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 );
}
Output:
P: FN
COM: FN
// A copy constructor call occurs when an object is added to a vector.
Since the output result is true, it can be concluded that the default copy constructor of A processes the space (1) used to point to the vtable pointer in the class object.
The method is to assign the vtable pointer of A to this space (1 ).
Instead of assigning the content of the Space P1 points to the vtable pointer (actually the vtable pointer of cpart) to this space (1)
// (1) indicates that the object specified by this phrase is the same