Is it wrong to write:
class A {
public:
virtual ~A() = 0;
};
for an abstract base class?
At least that compiles in MSVC... Will it crash at run time?
Is it wrong to write:
class A {
public:
virtual ~A() = 0;
};
for an abstract base class?
At least that compiles in MSVC... Will it crash at run time?
Yes. You also need to implement the destructor:
class A {
public:
virtual ~A() = 0;
};
inline A::~A() { }
should suffice.
If you derive anything from A and then try to delete or destroy it, A
's destructor will eventually be called. Since it is pure and doesn't have an implementation, undefined behavior will ensue. On one popular platform, that will invoke the purecall handler and crash.
Edit: fixing the declaration to be more conformant, compiled with http://www.comeaucomputing.com/tryitout/
A::~A()
, you should get a compile-time error—a link error. –
Gerah =default
the other special member methods as well? –
Relish virtual ~A() = 0 {};
. –
Stelle 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
};
= 0 { };
if the compiler accepts it? –
Curtain © 2022 - 2024 — McMap. All rights reserved.