指向派生类的基本指针,该派生类继承自多个基类

时间:2019-03-29 12:30:02

标签: c++ oop

考虑是否有这样的基类

class base1{
   protected:
   virtual void dosomething1(void)=0;
};

class base2{
  protected:
   void someprint(void){
      std::cout<<"someprint";
   };
};

class derived: public base1, public base2{

  public:
    void dosomething1(void){
       std::cout<<"do something 1";
    };
};



int main(){
   base1* ptr = new derived;
   ptr->dosomething1();
   return 0;
}

在这里,我们可以将基类ptr用于从2个基类继承的派生类吗?有效吗?

0 个答案:

没有答案