C++ function overriding

前端 未结 6 2250
独厮守ぢ
独厮守ぢ 2020-12-30 01:48

I have three different base classes:

class BaseA
{
public:
    virtual int foo() = 0;
};

class BaseB
{
public:
    virtual int foo() { return 42; }
};

clas         


        
6条回答
  •  清歌不尽
    2020-12-30 02:35

    In the derived class a method is virtual if it is defined virtual in the base class, even if the keyword virtual is not used in the derived class's method.

    • With BaseA, it will compile and execute as intended, with foo() being virtual and executing in class Child.
    • Same with BaseB, it will also compile and execute as intended, with foo() being virtual() and executing in class Child.
    • With BaseC however, it will compile and execute, but it will execute the BaseC version if you call it from the context of BaseC, and the Child version if you call with the context of Child.

提交回复
热议问题