为什么返回字符串&来自 const 方法无法编译?

发布于 2024-12-02 02:07:34 字数 599 浏览 2 评论 0原文

给出以下代码:

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

旧伤慢歌 2024-12-09 02:07:34

因为在 const 方法内部,所有非可变成员都是隐式 const。因此,您尝试将对非 const std::string (您的返回值)的引用绑定到 const std::string 类型的对象,即非法(因为它允许修改 const 数据),因此出现错误。

Because inside a const method, all non-mutable members are implicitly const. So, you're trying to bind a reference to non-const std::string (your return value) to an object of type const std::string, which is illegal(because it would allow modification of const data), hence the error.

放赐 2024-12-09 02:07:34

通过返回引用,您是说您可以修改引用变量隐式指向的类数据成员,从而修改该类...但是您已将类方法专用为常量方法,这意味着它不是允许更改任何未明确声明为可变的类成员变量。因此,通过返回非常量引用,您就破坏了类接口已建立的封装“契约”。您的选择是返回一个临时对象(即创建该对象的副本)或一个常量引用。所以你可以做

const string& getName() const {return m_first;}

或者

string getName() const { return m_first; } //copies m_first and returns the copy

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

const string& getName() const {return m_first;}

or

string getName() const { return m_first; } //copies m_first and returns the copy
作业与我同在 2024-12-09 02:07:34

您的代码承诺该引用不会更改 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

酒中人 2024-12-09 02:07:34

当你返回string &时,它允许修改类成员...但该函数是const,所以不允许出现这种情况。但是当你返回 const string & 时,它不允许修改类实例。

When you return string &, it allows modifying class member... but the function is const, so it is not allowed to allow such a situation. But when you return const string &, it doesn't allow modifying class instance.

巷子口的你 2024-12-09 02:07:34

如果你调用 A.getName().ModifyTheString() ==> 会怎么样?这意味着您修改了this

What if you call A.getName().ModifyTheString() ==> this means you modified this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文