Under what circumstances is it advantageous to give an implementation of a pure virtual function?

前端 未结 6 545
礼貌的吻别
礼貌的吻别 2020-12-05 13:37

In C++, it is legal to give an implementation of a pure virtual function:

class C
{
public:
  virtual int f() = 0;
};

int C::f() 
{
  return 0;
}

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-05 14:14

    Regarding the speed of the virtual destructor this is because the destructor is defined in the cpp file and not the header. It has more to do with size than speed. It is explained in detail in "Large-Scale C++ Software Design". Unfortunately I cannot remember all the details, but I think inline virtual functions get defined multiple times in the vtable.

    There is a discussion this here: Are inline virtual functions really a non-sense?

提交回复
热议问题