iamempty的专栏

全然分からない

C++多继承中二义性问题--虚基类

#include <iostream>
class Base
{
      public:
             Base()
             {
                   std::cout<<"Base Constructor"<<std::endl;
             }
             ~Base()
             {
                    std::cout<<"Base Deconstructor"<<std::endl;
             }
             void SetBase_Varible(int i)
             {
                  Base_Varible=i;
             }
             int GetBase_Varible()
             {
                  return Base_Varible;
             }
      private:
              int Base_Varible;
};
class DerivedOne:virtual public Base
{
      public:
             DerivedOne()
             {
                      std::cout<<"DerivedOne Constructor"<<std::endl;
                      SetBase_Varible(1);
             }
             ~DerivedOne()
             {
                      std::cout<<"DerivedOne Deconstructor"<<std::endl;
             }
};
class DerivedTwo:virtual public Base
{
      public:
             DerivedTwo()
             {
                      std::cout<<"DerivedTwo Constructor"<<std::endl;
                      SetBase_Varible(2);
             }
             ~DerivedTwo()
             {
                      std::cout<<"DerivedTwo Deconstructor"<<std::endl;
             }
};
class DerivedThree:public DerivedOne,DerivedTwo
{
      public:
             DerivedThree()
             {
                      std::cout<<"DerivedThree Constructor"<<std::endl;
                      std::cout<<GetBase_Varible()<<std::endl;
             }
             ~DerivedThree()
             {
                      std::cout<<"DerivedThree Deconstructor"<<std::endl;
             }
};
int main()
{
    DerivedThree *x=new DerivedThree();
    delete x;
    std::system("PAUSE");
    return 0;
}

阅读更多
想对作者说点什么? 我来说一句

多继承虚基类PPT

2008年11月18日 266KB 下载

没有更多推荐了,返回首页

不良信息举报

C++多继承中二义性问题--虚基类

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭