I've Googled this and read many posts, but there are so many different answers that all make logical sense that I was wondering if an expert on the topic could demystify this question.
Some say that there is no return because there is no way to return - the syntax prohibits it - yes, this makes sense, but I believe that all functions have to return something, no? Others say that the constructor sort of returns the newly created object itself, which seems to make sense since the assignment operator is used on the constructor. Still others have other interesting explanations.
The constructor does not specify a return type because it would be redundant: there is no type other than the one being constructed that a constructor could potentially "return". I put "return" in quotes because technically constructors do not return anything: when they are invoked in a static context, they initialize an instance in place; when they are invoked in a dynamic context, it is the operator
new
that returns something, not a constructor.Constructors aren't called like other functions, so they don't return like other functions. They execute as a side-effect of certain constructs (cast,
new
, variable definition, ctor-initializer-list, pass-by-value, return-by-value).Assuming constructors could return something, then this has problematic implications for the following function call:
Preventing constructor functions from returning, facilitates the use of anonymous temporaries, along with many more C++ features. Without such a rule, innocuous statements can become ambiguous:
The ability for constructors to return a value, complicates the creation of objects - having a single unambiguous type, the class name, in the absence of arbitrary return values, avoids such problems.
Can readers suggest further examples where C++ idioms are hindered by the absence of such a rule?
The constructor constructs in-place, it has no need to return anything.
void
?Constructors are perhaps a bad name for it. It is practically an initializer for pre-constructed objects. Easiest way to illustrate this is a pseudo-example for the equivalent construct without using the C++ constructors.
With constructors
Without constructors
Now, if you imagine that
X::initialize
in the second example were to be made to return, say abool
indicating success or failure, you would have a problem. Inmain()
, the inattentive programmer might end up with anX
that isn't properly initialized, leading to undefined behavior (usually a hard-to debug crash that might not be discovered before production).This is one of the core reasons why constructors are made special. The only two ways to exit a constructor, is through normal completion, or through an exception, which must then be handled by the caller (or passed on up the stack). In any case, you prevent ending up with uninitialized objects.
As a side-note, uninitialized objects are one of the more common causes of bugs in C, which does not have anything to help the programmer like this.
That constructors lack a return type -- not evenvoid
-- means that you can't call a constructor from your C++ code. And that's the point. Calling a constructor doesn't make sense. Making it illegal to call a constructor eliminates the very possibility of this type of user error.Edit
barnes is right in that the ultimate reason you can't call constructors is that they have no name. (And even this ignores that you can call a constructor indirectly via placement new.)
Trying again:
You can't call constructors from your C++ code because constructors don't have a name. Making constructors not have a return type emphasizes to the programmer that constructors are a very different kind of function than other functions. What a constructor actually does return, if it returns anything, is up to the vendor.