Default pure virtual destructor
Asked Answered
S

1

48

In C++11, we are able to declare a destructor to be auto generated:

struct X {
  virtual ~X() = default;
};

Also, we can declare a destructor to be pure virtual:

struct X {
  virtual ~X() = 0;
};

My question is: how to declare the destructor to be both auto generated and pure virtual? Looks like the following syntax is not correct:

struct X {
  virtual ~X() = 0 = default;
};

Neither is this one:

struct X {
  virtual ~X() = 0, default;
};

Nor this one:

struct X {
  virtual ~X() = 0 default;
};

EDIT: Some clarification on the purpose of the question. Basically I want an empty class to be non-instantiable base class, but derived class is instantiable, then the class must have a pure virtual destructor. But on the other hand, I don't want to provide the definition in a .cpp file. So I need some sort of mechanism equivalent to default. I wonder if anyone has an idea to solve the problem.

Siamang answered 15/7, 2012 at 18:17 Comment(0)
V
60

In order to define a pure virtual method, you need a separate definition from the declaration.

Therefore:

struct X {
    virtual ~X() = 0;
};

X::~X() = default;
Verbiage answered 15/7, 2012 at 18:54 Comment(14)
Here's a link to an Ideaone example that successfully compiles.Angularity
darn, you beat me to it while I was getting my copy of effective C++ (where this is shown using the old syntax)Rhombic
This answer is incorrect! You still need to put the line X::~X() = default; into the .cpp file, otherwise there will be multiple definition linking error.Siamang
@icando unless you write inline; That's not implied when default is it?Wrenn
@Dave: it is not, but all methods defined within the class scope are inline by default so it is easy to get confused.Verbiage
And here is an updated ideaone example based on the @nicol-bolas code. I also edited the answer, but StackOverflow warns me This edit will be visible only to you until it is peer reviewed.Aveyron
@vinipsmaker: Your edit was actually rejected, though it's borderline here. I prefer not to put the inline in this answer, as it is necessary in a header file but useless in a source file. In the end, therefore, I think this is an orthogonal issue (since here X::~X could be defined in either), and it might actually confuse the reader into thinking inline is always mandatory when defining pure virtual methods, which is not the case obviously.Verbiage
@MatthieuM. no problem. And I agree it is an orthogonal problem. All discussion prior to my comment is enough for any reader.Aveyron
It seems like a silly semantic issue that a definition has to be separated from its declaration simply to use these 2 together.Listerism
@void.pointer: I would even go so far as a silly syntactic issue! :)Verbiage
That would also be an appropriate complaint! Good answer!Listerism
I didn't know that delete/default keyword could be used inside cpp file as well.Caterinacatering
@nurabha: Then I am glad to have provided you with this nugget of information :)Verbiage
Does anyone know why C++ doesn't generate a default implementation for pure virtual destructors, and doesn't let you use the default keyword either ?Malloy

© 2022 - 2024 — McMap. All rights reserved.