为什么返回字符串&来自 const 方法无法编译?
给出以下代码:
#include <iostream>
#include <string>
using namespace std;
class A
{
private:
string m_name;
string m_first;
public:
A(): m_first("string") {}
virtual void print() const {}
string& getName() const {return m_first;} // won't compile
const string& getLastName() const {return m_name;} // compile
};
int main()
{
A a;
return 0;
}
编译器呈现:“类型‘std::string&’引用的初始化无效来自“const std::string”类型的表达式
为什么我不能从 getName() 返回“m_first”?我认为函数尾部的 const 表明该函数不会更改“this”...但我并不想更改 this ,只是返回一个数据成员。
Given the following code:
#include <iostream>
#include <string>
using namespace std;
class A
{
private:
string m_name;
string m_first;
public:
A(): m_first("string") {}
virtual void print() const {}
string& getName() const {return m_first;} // won't compile
const string& getLastName() const {return m_name;} // compile
};
int main()
{
A a;
return 0;
}
Compiler presents : "invalid initialization of reference of type 'std::string&' from expression of type 'const std::string'"
Why can't I return "m_first" from getName() ? I thought that the const on the tail of the function states that the function will not change 'this'... but I'm not trying to change this , just return a data member.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
因为在 const 方法内部,所有非可变成员都是隐式 const。因此,您尝试将对非 const
std::string
(您的返回值)的引用绑定到const std::string
类型的对象,即非法(因为它允许修改 const 数据),因此出现错误。Because inside a const method, all non-
mutable
members are implicitlyconst
. So, you're trying to bind a reference to non-conststd::string
(your return value) to an object of typeconst std::string
, which is illegal(because it would allow modification of const data), hence the error.通过返回引用,您是说您可以修改引用变量隐式指向的类数据成员,从而修改该类...但是您已将类方法专用为常量方法,这意味着它不是允许更改任何未明确声明为可变的类成员变量。因此,通过返回非常量引用,您就破坏了类接口已建立的封装“契约”。您的选择是返回一个临时对象(即创建该对象的副本)或一个常量引用。所以你可以做
或者
By returning a reference, you are saying you can modify the class data-member that the reference-variable is implicitly pointing to, and therefore modify the class ... but you have dedicated the class method as a constant method, meaning it is not allowed to change any class member variables that have not been specifically declared mutable. So by returning a non-constant reference, you are breaking the encapsulation "contract" that the class interface has established. Your options are to either return a temporary object (i.e., that creates a copy of the object), or a constant-reference. So you could either do
or
您的代码承诺该引用不会更改 m_name 成员,但您返回一个可以更改它的引用。你想要的是一个字符串 const&返回类型。
这将返回对 m_name 的“只读”引用。
另请参阅:有关 const 正确性的 C++ 常见问题解答
Your code promises that the reference won't change the m_name member, but you return a reference that can change it. What you want is a string const& return type.
This returns a "read-only" reference to m_name.
See also: the C++ FAQ on const correctness
当你返回
string &
时,它允许修改类成员...但该函数是const
,所以不允许出现这种情况。但是当你返回 const string & 时,它不允许修改类实例。When you return
string &
, it allows modifying class member... but the function isconst
, so it is not allowed to allow such a situation. But when you returnconst string &
, it doesn't allow modifying class instance.如果你调用
A.getName().ModifyTheString()
==> 会怎么样?这意味着您修改了this
。What if you call
A.getName().ModifyTheString()
==> this means you modifiedthis
.