Why can't we return anything from the constructor?
C++ says that we can't return anything from the constructor? What is the historical reason behind开发者_高级运维 it? Why did Bjarne disallow contructors to return something unlike any other member function?
Because when an object is being constructed in a new
statement or in a variable initializer, the object that is returned is that new object which is being constructed. What would you ever do with an object returned from a constructor? It couldn't ever be returned anywhere; it's the object being constructed that is returned. That object has already been (partially) created before the constructor is called (otherwise, the constructor wouldn't have an object to work on), and it must be what is returned from the constructor, so there is no point in making the user return it or allow them to confuse themselves by trying to return something different.
I think of a constructor as returning itself, but without having to have a return
statement.
If it returns itself, then it cannot return anything else.
I can't speak for Bjarne, but the idiomatic way to look at a constructor is that it is returning the object it constructed.
Aside from the fact that having the constructor return something would give constructors two things to do instead of just one (which is generally an undesirable thing), note that constructors can't be named so it is impossible to call them explicitly.
As it's impossible to construct an expression that is an explicit call to a constructor, it would be impossible to assign or refer to a constructor's return value in any case.
Attempting to return something from a constructor just doesn't fit with the language design at all.
Constructors don't "return" objects, they initialize objects in the memory area in which they are invoked. If you declare an object of class type with static storage duration (and that type has a user-declared constructor), the memory is reserved for the lifetime of the program and the implementation ensures that the constructor is called to initialize the object at that location at the appropriate time in the program.
Similarly, if you declare and object with automatic storage duration, the implementation reserves space (informally some stack space) and invokes the constructor each time the declaration statement is executed.
In the case of a new expression, the memory is allocated dynamically and the implementation calls the appropriate constructor to initialize the object.
Note that if X
is a class type, the X
in all of these statements always refers to the name of the type and never the constructor which doesn't actually have a name. The syntax for declaring or defining a constructor is special, it doesn't mean that the constructor has a name. Assume that X
has a user-declared constructor.
X x; // Initialize an object of type X using the default constructor
X(); // Value initialize a temporary of type X. Not an explicit constructor call.
new X(); // new expression: value-initialize a dynamically allocated X
X(a); // A function style cast
X(a, b); // Construct an X from the expression list. X must have a
// suitable constructor but the X still refers to the type.
The constructor can only yield the object itself...
Constructor don't have retunrn type bacause it already returns the refrence_id to refrence variable(which is called as an object) for ex:- Emp e1 = new Emp(refrence_id) here Emp is the class_name e1 is the refrence variable new is used for dynamic allocation Emp() is the constructor of class Emp in this constructor returns the refrence_id to refrence variable e1
精彩评论