Influence of the right value on parameter passing

Source: Internet
Author: User

Let's take a look at the following code:
[Cpp]
Class T
{
Public:
Int;
};
Void d (T & e)
{
Printf ("% d \ n", e. );
}
 
D (T ());
We don't want to use another variable to directly instantiate the object and pass it into the function. This method is acceptable in java and c # (they pass references, meaning is valid ). The following error occurs during compilation in C ++:
Invalid initialization of non-const reference of type't & 'from an rvalue of type 'T'
It produces an error in initializing a very large number of references from a right value.
Here we have a concept of the right value. What is the right value?
A value is either the left or the right. In C ++, the right semantics is evolved into a non-left value or the right value. What is the left value (expression )? The left value is an expression with a name or reference (note: the pointer does not work ).
Because the temporary pair generated by class t through t () is like a right value, rather than the reference of const cannot reference the right value, the above error is generated.
If yes
[Cpp]
Void d (const T & e ){}
D (T ());
There is no error. In the c ++ standard, the const type reference can be the right value.
If yes
[Cpp]
Void d (T * e) {}// no matter from time to time const
D (new T ());
There is no error because the pointer is the right value.
Source

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.