C++ function overriding

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

    From a polymorphism point of view, prefer A, so you know each child has his own implementation of the virtual function.
    Choose B mainly if you have a valid default implementation, but then you have to make sure that all child-classes have their own implementation as needed. C is not polymorphism, so use judiciously.

提交回复
热议问题