Please tell me why the constructor does not return any value. I want a perfect technical reason to explain to my students why the constructor does not have any return type.
标签:
constructor
相关问题
- In C++ are constructors called before or after obj
- Forcing CodeIgniter to send view and stop working
- New instance has old instance attribute values [du
- C++ base class constructor taking derived class as
- How function resolution in prototype chain will wo
相关文章
- Java call constructor from constructor
- What CursorAdapter have I to use?
- is Object.getPrototypeOf() same as Object.construc
- F# Object Initialization with a Constructor
- Why the copy constructor is not called?
- How to pass std::map as a default constructor para
- Initialization in polymorphism of variables
- In Java, when is the constructor for an enumerated
In case of C#, the syntax for declaring object is :
According to this line, if we are using assignment operator(=) then it should return some value. But the main objective of a constructor is to assign values to variables, so when we use a new keyword it creates instance of that class, and constructor assigns values to the variable for that particular instance of object, so constructor returns assigned values for that objects's instance.
(I'm biased towards C++, so regarding other languages, take this with a grain of salt.)
Short answer: You don't want to have to explicitly check for success for every single object construction in your code.
Somewhat longer answer: In C++, constructors are called for dynamically as well as for globally and automatically allocated objects. In this code
there is no way for the constructor of
s
(std::string::string()
) to return any value. Either it succeeds - then we can use the object, or it throws an exception - the we never get a chance to try to use it.IMO, that's the way it should be.
How is a constructor supposed to return a return value? The
new
operator returns the newly created instance. You do not call a ctor,new
does it.If the ctor would return a value, like so:
Where would you receive the integer?
Even though the VM implementation of a constructor isn't to return any value, in practice it kind of does - the new object's reference. It would then be syntactically weird and / or confusing to be able to store one or both of the new object's reference and an additional return value in one statement.
From what I know about OO design methodologies, I would say the following:
1)By allowing a constructor to return a value, framework developer would allow the program to crash in an instant where the returned value is not handled. To keep the integrity of the program workflow, not allowing a return value from the initialization of an object is a valid decision. Instead, language designer would suggest/force the coders to use getter/setter - access methods.
2)Allowing the object to return a value on initialization also opens possible information leaks. Specially when there are multiple layer or access modifications applied to the variables/methods.
I would be using Java as my language in the answer.
Now let us see what is happening here. in java, we use the new keyword to create an instance of a class. And as you can see in the code, in the line, SayHelloOnCreation thing = new SayHelloOnCreation();, the expression after the assignment operator runs before assignment is done. So using the keyword new, we call the constructor of that class (SayHelloOnCreation()) and this constructor creates an object on the Java Heap. After the object is created, a reference to that object is assigned to the thing reference of type SayHelloOnCreation.
The point that I am trying to keep here is that if constructors were allowed to have a return type, Firstly the strongly typed nature of the language would be compromised (Remember I am speaking about Java here).
Secondly, an object of class SayHelloOnCreation is created here so by default I guess the constructor returns a reference of the same type, to avoid ClassCastException.