C++ function overriding

前端 未结 6 2231
独厮守ぢ
独厮守ぢ 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:21

    It mostly depends on how you called it.

    if you did:

    class Child : public BaseA
    {
    public:
        int foo() { return 42; }
    };
    

    and did

    BaseA baseA = new Child();
    baseA->foo();
    

    It would call Child's foo function.

    However, if you did this:

    BaseA baseA = new BaseA();
    

    It would produce a compile time error.

提交回复
热议问题