问题描述
错误的形式:
int& z = 12;
正确的形式:
int y;
:
int& r = y;
为什么第一个代码错误?解决方案标题中的错误意义是什么? / 1说:每个表达式都是左值或右值。重要的是要记住,lvalueness和rvalueness是表达式的属性,而不是对象的属性。
Lvalues名称对象持续超过单个表达式。例如,obj,* ptr,ptr [index]和++ x都是lvalue。
Rvalues是临时表达式,他们住(在分号)。例如,1729,x + y,std :: string(meow)和x ++都是右值。
操作符的地址要求其应为左值。如果我们可以取一个表达式的地址,表达式是一个左值,否则它是一个右值。
& obj; // valid
& 12; // invliad
Wrong form:
int &z = 12;
Correct form:
int y; int &r = y;
Question:
Why is the first code wrong? What is the "meaning" of the error in the title?解决方案C++03 3.10/1 says: "Every expression is either an lvalue or an rvalue." It's important to remember that lvalueness versus rvalueness is a property of expressions, not of objects.
Lvalues name objects that persist beyond a single expression. For example, obj , *ptr , ptr[index] , and ++x are all lvalues.
Rvalues are temporaries that evaporate at the end of the full-expression in which they live ("at the semicolon"). For example, 1729 , x + y , std::string("meow") , and x++ are all rvalues.
The address-of operator requires that its "operand shall be an lvalue". if we could take the address of one expression, the expression is an lvalue, otherwise it's an rvalue.
&obj; //valid &12;//invliad
这篇关于错误:类型'int'的类型'int&'的非const引用的初始化无效'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!