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

前端 未结 6 542
礼貌的吻别
礼貌的吻别 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:25

    Because it's regarded as ill formed to write:

    class Funct {
    public:
      virtual int doit(int x) = 0;
      virtual ~Funct() = 0 {};
    };
    

    The destructor will still be called if you derive from this class. Declaring all methods pure virtual is just for clarity. You might as well write it like this:

    class Funct {
    public:
      virtual int doit(int x) = 0;
      virtual ~Funct() {};
    };
    

    The class will still be abstract since at least one method is pure virtual. The destructor is also still inline.

提交回复
热议问题