views:

205

answers:

4

I think virtual alone is generally sufficient.

Is there another reason to make it pure virtual than to force derived classes to implement their own destructor? I mean if you allocate something in your class' constructor you should impement your own destructor - if your class is derived or not.

Doesn't count as answer as I already know: If you want your class abstract and it has no pure virtual functions - leave it to the destructor.

Some more uses?

+9  A: 

No. If the base class allocates anything, it is it's responsiblity to release it.

Further, if the derived class does not allocte anything, there's no point in forcing them to write a dummy dtor.

James Curran
You don't force them to anything. If they don't define destructor, compiler will do it for them.
Tadeusz Kopec
@Tadeusz: Even if it's a pure virtual in the base?
James Curran
Yes - tested with gcc 3.4.5. Question: Is that C++ standard conform?
spc-mrn
Also, the first point is irrelevant; the base class destructor must have an implementation, even if it is pure virtual.
Mike Seymour
Standard 12.4.7 "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. If a class has a base class with a virtual destructor, its destructor (whether user- or implicitly- declared) is virtual."
Tadeusz Kopec
A: 

If your abstract class is a pure interface, with no data members than you could get along with making the dtor pure virtual. I prefer that myself, since I've seen so many hot-shot programmers forget to make a virtual destructor at all: Even when they write derived classes containing virtual methods. So I would do it purely to minimize maintenance headaches down the road.

C Johnson
Forgetting to write destructors is a risk in any class, not just derived classes! The biggest problem here is forgetting to write a virtual destructor in the *base* class.
Oli Charlesworth
I'm not sure why there was a down vote? since there was no comments as to why. So that isn't very helpful - if I'm missing something. Or If I didn't articulate myself very well (Which I see i could have used a proof-reader this morning).
C Johnson
+1  A: 

Ideally the language should have a way to assure (implicitly or not) that the destructor is virtual in abstract classes without having to define it or make it pure. But it hasn't.

So the choice is: either make it pure, and have the burden of defining it in each derived class, or make it not, and have the burden of defining it in the abstract class. The later is less work, and also shorter code, so I'd go for it.

Fabio Ceconello
Shorter code because the pure virtual dtor has to be implemented outside the class. The first argument against pure virtual! Thanks
spc-mrn
Actually, I don't thing the code would get shorter. Compare `~Klass() =0` + its implementation and `~Klass() {}`.
jpalecek
"+ its implementation" is the point as it cannot be inline, i.e. Klass::~Klass(){}
spc-mrn
@jpalacek, what I meant is that if the destructor is not pure in the abstract class, you don't have to define it in each derived class. So you don't have the extra destructor in the call chain. Also it's less clutter in the derived classes.
Fabio Ceconello
@Fabio: even if it's pure in the base class, you don't have to define it in each derived class. The implicit destructor is good enough.
Mike Seymour
You're right, good point.
Fabio Ceconello
+5  A: 

If you want your class abstract and it has no pure virtual functions - leave it to the destructor.

Actually, I don't think there's more. All the pure virtual destructor does, is make the whole class abstract. You have to provide the implementation for the pure virtual destructor as well as for a non-pure virtual destructor, the destructors of the derived classes are virtual with virtual destructor alone, etc.

Basically, if a class has already some pure virtual functions, its behaviour would be equivalent with virtual and pure-virtual destructor.

jpalecek
+1 for the only correct answer so far.
Mike Seymour
It indeed is the answer, which matches my question best.
spc-mrn