class DiamondBase { public: int DiamondBaseData1; }; // virtual base class will share base-class-subobject with DiamondDerived2 class class DiamondDerived1: virtual public DiamondBase { public: int DiamondBaseData2; void DiamondDerived1Fun(void){}; }; // virtual base class will share base-class-subobject with DiamondDerived1 class class DiamondDerived2: virtual public DiamondBase { public: int DiamondBaseData3; void DiamondDerived2Fun(void){}; }; class VirtualBaseDerived: public DiamondDerived1, public DiamondDerived2 { public: int DiamondBaseData4; };
void Main() { DiamondDerived2 obj1; DiamondBase* pBase = &obj1; // upcasting of DiamondDerived2 ((DiamondDerived1*)pBase)->DiamondDerived1Fun(); // error: cannot convert from base to derived via "virtual-base" ((DiamondDerived2*)pBase)->DiamondDerived2Fun(); // error: cannot convert from base to derived via "virtual-base" }
dynamic_cast
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)