菱形继承会存在多份数据的问题,使用虚继承解决问题
虚基类指针vbptr
#include <iostream> class Animal{ public : int m_Age; }; class Sheep:virtual public Animal{ public: Sheep(){ std::cout<<"sheep"<<std::endl; }; }; class Tuo:virtual public Animal{ public: Tuo(){ std::cout<<"tuo"<<std::endl; }; }; class SheepTuo:public Sheep,public Tuo{ public: SheepTuo(){ std::cout<<"SheepTuo"<<std::endl; } }; void test01(){ SheepTuo sheeptuo; std::cout<<sheeptuo.Sheep::m_Age<<std::endl; std::cout<<sheeptuo.m_Age<<std::endl; } int main(){ return 0; }
标签:Sheep,继承,菱形,Animal,基类,public From: https://www.cnblogs.com/gishuanhuan/p/18106935