C++ empty constructor/destructor questions

Started by
16 comments, last by SiCrane 15 years, 4 months ago
Quote:Original post by Guthur
I think its refering to the destructor in the derived class; it doesn't need to be explicitly defined as virtual, its implied from the base class.
However, if you use that derived class as a polymorphic base class itself, you might be better off explicitly defining the virtual destructor. I say this because several gcc/ld combinations seem to mess this case up, particularly across DLL boundaries.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Advertisement
Ya I would most likely be explicit about it myself; just clarifying the article :)
Innovation not reiterationIf at any point I look as if I know what I'm doing don't worry it was probably an accident.
Quote:Original post by Bregma
Quote:Original post by iMalc
... and you delete the default constructor ...

How would one accomplish that neat trick?
Oh was my terminology a little off?
I mean the constructor that takes no arguments, the one that gets called when you don't provide any arguments. If you've written one of those you find it and press the delete key.
So what should I have called it?
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
Quote:Original post by iMalc
Oh was my terminology a little off?
I mean the constructor that takes no arguments, the one that gets called when you don't provide any arguments. If you've written one of those you find it and press the delete key.
So what should I have called it?


You are correct it is default constructor but i think what he means is you can delete it all you want, the compiler will still generate a default constructor so it can't be deleted per se :)
Innovation not reiterationIf at any point I look as if I know what I'm doing don't worry it was probably an accident.
Quote:Original post by Guthur
Quote:Original post by iMalc
Oh was my terminology a little off?
I mean the constructor that takes no arguments, the one that gets called when you don't provide any arguments. If you've written one of those you find it and press the delete key.
So what should I have called it?


You are correct it is default constructor but i think what he means is you can delete it all you want, the compiler will still generate a default constructor so it can't be deleted per se :)
But that's just it, C++ doesn't generate the default constructor if you have defined other constructors.

Minimal example:
struct X {	X() {} // Try deleting this	X(int y) {}};X x;
Do people not know this, or was original post misread, or am I just not explaining it right?
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
iMalc I stand corrected, it does indeed ask for a default to be defined if you have a non default constructor specified but use default constuction. Heres something else though, if you add the () it will compiler eg. A a();, but it will warn that no function of that prototype was found just a useless tibbit :)

[Edited by - Guthur on December 5, 2008 6:04:38 AM]
Innovation not reiterationIf at any point I look as if I know what I'm doing don't worry it was probably an accident.
Quote:Original post by iMalc
Do people not know this, or was original post misread, or am I just not explaining it right?

All three, since evidently some people don't know that provide a contstructor, any constructor, will cause the automatically generated ones to not be automatically generated.

My confusion came because I didn't realize you meant that if you had already written a default constructor as well as other constructors, and you then delete the default constructor you had already written, then you will no longer have a default constructor. I gather this might be an artefact of using Visual Studio, like using a single unnamed void parameter instead of an empty parameter list.

The new C++09 language provides a way to explicitly disable automatically generated constructors (and operators). I thought maybe there was a technique in the existing language that did the same thing that I might be unaware of.

Stephen M. Webb
Professional Free Software Developer

Quote:Original post by Bregma
All three, since evidently some people don't know that provide a contstructor, any constructor, will cause the automatically generated ones to not be automatically generated.

No, it's just the default constructor. The compiler will always generate the copy constructor unless you define it yourself even if you provide other constructors.

This topic is closed to new replies.

Advertisement