简体   繁体   中英

Will the following program cause any problem during compiling and execution process?

Will the following program cause any problem during compiling and execution process?

class A{
  public: virtual void foo(){}
};

class B:public A{};

int main(){
    B b;
    b.foo();
}

我猜想,也许他们正在测试您是否知道虚拟抽象之间的区别?

There will be no problems compiling or running this program.

virtual functions can be overridden, but they don't have to be. If an object's class does not implement the virtual function, then the superclass will be checked for an implementation.

Looks fine to me. What problem do you think it causes?

Why would it have a problem? You're calling a virtual function that IS defined on the parent class. B inherits it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM