In C ++, why cannot a constructor return values?

Source: Internet
Author: User

In C ++, why cannot a constructor return values? (1) Suppose there is a class C, as defined below: class C {public: C (): x _ (0) {} C (int I): x _ (I) {} private: int x _;}; if the constructor of C can return values, for example, int: int C (): x _ (0) {return 1; // 1 indicates successful construction and 0 indicates failed.} what will happen to the following code? C c = C (); // c. x _ = 1 at this time! Obviously, C () calls the non-parameter constructor of C. The constructor returns the int value 1. Exactly C has a constructor C (int I ). As a result, chaos is coming. According to C ++, C c = C (); uses the default constructor to create a temporary object and initializes c with this temporary object. In this case, the value of c. x _ should be 0. However, if C: C () has a return value and 1 is returned (to indicate success), C ++ will use 1 to initialize c, it is called but the parameter constructor C: C (int I ). The obtained c. x _ is 1. Therefore, semantics produces ambiguity. This makes C ++ very complex syntax and further messy. The reason why a constructor call does not return values is determined by the special nature of the constructor. In terms of basic semantics, the constructor returns the constructed object. Otherwise, we will not be able to use the temporary object: void f (int ){...} // (1) void f (const C & ){...} // (2) f (C (); // (3), who is called? For (3), what we want to call is (2). But if C: C () has an int type return value, it would be better to call (1, or call (2. Therefore, our heavy load system and even the entire syntax system will crash. The core here is the expression type. Currently, expression C () is of the Class C. However, if C: C () has a return type R, the type of expression C () should be R rather than C, which leads to the above type problem. (2) Only the C ++ Standard specifies that the return type cannot be specified for the constructor/destructor/custom type conversion character. However, you cannot say that they have no return type. (3) My opinion is that the constructor has a returned value. The returned value is the newly constructed object, but the return type cannot be specified, because you use the constructor of this class to indicate that it is to return an object of this class, and there is no need to specify the return type. Even if it is specified, it must be the return type of the specified class, this is exactly the same thing.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.