Why do constructors not return values?

2019-01-02 17:15发布

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
17条回答
路过你的时光
2楼-- · 2019-01-02 17:31

In case of C#, the syntax for declaring object is :

classname objectname= new constructor();

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.

查看更多
低头抚发
3楼-- · 2019-01-02 17:35

(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

void f()
{
  std::string s;
}

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.

查看更多
无色无味的生活
4楼-- · 2019-01-02 17:36

How is a constructor supposed to return a return value? The new operator returns the newly created instance. You do not call a ctor, newdoes it.

MyClass instance = new MyClass();

If the ctor would return a value, like so:

public int MyClass()
{
    return 42;
}

Where would you receive the integer?

查看更多
荒废的爱情
5楼-- · 2019-01-02 17:36

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.

查看更多
墨雨无痕
6楼-- · 2019-01-02 17:36

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.

查看更多
栀子花@的思念
7楼-- · 2019-01-02 17:36

I would be using Java as my language in the answer.

class SayHelloOnCreation {
     public SayHelloOnCreation() {
         System.out.println("Hello, Thanks For Creating me!");
     }
}

class Test {
     public static void main(String[]args) { 
         SayHelloOnCreation thing = new SayHelloOnCreation(); //This line here, produces an output - Hello, Thanks For Creating me!
     }
}

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.

查看更多
登录 后发表回答