虛擬單一繼承下基類和衍生類別起始地址不同.
// VC++2005<br />#include <stdio.h> </p><p>class Base<br />{<br />};</p><p>class Derived: public Base<br />{<br />};</p><p>class VirtualDerived: virtual public Base<br />{<br />};</p><p>int main()<br />{<br /> Base* pB = NULL; </p><p> Derived* pD = new Derived;<br /> pB = pD;<br /> printf("sizeof(Base) = %u/n", sizeof(Base));<br /> printf("sizeof(Derived) = %u/n", sizeof(Derived));<br /> printf("pD is 0x%p./n", pD);<br /> printf("pB is 0x%p./n", pB);<br /> delete pD;<br /> pD = NULL;<br /> pB = NULL; </p><p> printf("/n"); </p><p> VirtualDerived* pVD = new VirtualDerived;<br /> pB = pVD;<br /> printf("sizeof(Base) = %u/n", sizeof(Base));<br /> printf("sizeof(VirtualDerived) = %u/n", sizeof(VirtualDerived));<br /> printf("pVD is 0x%p./n", pVD);<br /> printf("pB is 0x%p./n", pB);<br /> delete pVD;<br /> pVD = NULL; </p><p> return 0;<br />} // end of main // 控制台輸出<br />sizeof(Base) = 1<br />sizeof(Derived) = 1<br />pD is 0x003B5EF0.<br />pB is 0x003B5EF0.</p><p>sizeof(Base) = 1<br />sizeof(VirtualDerived) = 4<br />pVD is 0x003B5EF0.<br />pB is 0x003B5EF4.