Quote:Original post by Bregma
The answer is that functions can return values. Constructors and destructors are not functions. They cannot return values. It's just unfortunate that their definitions use a syntax apparently similar to that of functions. Don't confuse syntax with semantics.
I can think of only three ways they differ from functions:
1) No return type
2) Initializer lists
3) The user isn't supposed to normally call them directly
(3) doesn't make much of an argument, because it's just like saying "don't call free() twice in a row on the same pointer". (2) is a hack that may or may not justify adding something conceptually different. (1), well, I've already covered that.
In fact, from the C++ FAQ Lite, I see this code:
void someCode() { char memory[sizeof(Fred)]; void* p = memory; Fred* f = new(p) Fred(); ... f->~Fred(); // Explicitly call the destructor for the placed object }
which tells me that at least destructors can be called like functions. Destructors also hang out with other functions in the vtable if you declare them virtual.They may indeed not be functions, but it seems an even subtler distinction than that between arrays and pointers.
So, you've cleared up a misunderstanding of mine (c/dtors are not functions), but that doesn't change the "heart" of the question. I'll rephrase to, "Why aren't constructors/destructors functions?"