错误:将限定符放在类型x的绑定引用与类型y的初始化程序中 [英] Error: Qualifiers dropped in binding reference of type x to initializer of type y

查看:94
本文介绍了错误:将限定符放在类型x的绑定引用与类型y的初始化程序中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下原因为何引发此错误:

why does the following throw this error:

IntelliSense:限定符已放入类型的绑定引用中字符串和"到常量字符串"类型的初始化程序

IntelliSense: qualifiers dropped in binding reference of type "string &" to initializer of type "const string"

.h

class A
{
public:
    wstring& GetTitle() const;
private:
    wstring title;    
};

.cpp

wstring& GetTitle() const
{
    return this->title;
}

如果我删除了const字,它会停止抱怨,但是我从未对变量进行过任何更改吗?

If i remove the const word, it stops complaining, and yet i have never made any changes to the variable?

推荐答案

通过返回对类成员的非常量引用,您可以使调用者访问该对象,就好像它是非常量一样.但是 GetTitle 作为const函数,无权授予该访问权限.

By returning a non-const reference to a member of your class, you are giving the caller access to the object as if it's non const. But GetTitle, being a const function, does not have the right to grant that access.

例如:

A a;
string& b = a.GetTitle(); // Allows control over original variable

这篇关于错误:将限定符放在类型x的绑定引用与类型y的初始化程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆