string foo() { return "hello"; }
int main()
{
//below should be illegal for binding a non-const (lvalue) reference to a rvalue
string& tem = foo();
//below should be the correct one as only const reference can be bind to rvalue(most important const)
const string& constTem = foo();
}
Run Code Online (Sandbox Code Playgroud)
std::string&从类型的临时类型无效初始化类型的非const引用std::stringstd::string到std::string &非const引用可以仅被绑定到一个左值&&,而是在演示代码中,我只是使用非const左值引用!可以用somone帮我解释一下VS2010的行为吗?这是一个错误!?谢谢
我是 C++ 的新手,遇到了const关键字。我在网上查了一下,读到主要用途是防止在整个程序中更改值。我在下面有这两个片段
const int size = 56;
Run Code Online (Sandbox Code Playgroud)
与使用相比
int size = 56;
Run Code Online (Sandbox Code Playgroud)
为什么我应该使用一个?使用它背后的想法是什么。