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