Pure virtual destructor in C++

后端 未结 2 1413
遥遥无期
遥遥无期 2020-11-22 13:21

Is it wrong to write:

class A {
public:
    virtual ~A() = 0;
};

for an abstract base class?

At least that compiles in MSVC... Will

2条回答
  •  北恋
    北恋 (楼主)
    2020-11-22 13:21

    Private destructors: they will give you an error when you create an object of a derived class -- not otherwise. A diagnostic may appear though.

    12.4 Destructors

    6 A destructor can be declared virtual (10.3) or pure virtual (10.4); if any objects of that class or any derived class are created in the program, the destructor shall be defined.

    A class with a pure virtual destructor is an abstract class. Note well:

    10.4 Abstract classes

    2 A pure virtual function need be defined only if called with, or as if with (12.4), the qualified-id syntax (5.1).

    [Note:a function declaration cannot provide both a pure-specifier and a definition —end note ]

    Taken straight from the draft:

    struct C {
       virtual void f() = 0 { }; // ill-formed
    };
    

提交回复
热议问题