const 函数的编译器错误
我不确定我是否缺少一些基本的东西。 但我无法理解为什么编译器会为此代码生成错误:
class A
{
};
class B
{
public:
B();
A* get() const;
private:
A* m_p;
};
B::B()
{
m_p = new A;
}
A* B::get() const
{
//This is compiling fine
return m_p;
}
class C
{
public:
A* get() const;
private:
A m_a;
};
A* C::get() const
{
//Compiler generates an error for this. Why?
return &m_a;
}
编辑:编译器错误是:错误 C2440:'return':无法从 'const class A *' 转换为 'class A *' 转换丢失限定符
I am not sure whether I am missing something basic. But I am unable to understand why the compiler is generating the error for this code:
class A
{
};
class B
{
public:
B();
A* get() const;
private:
A* m_p;
};
B::B()
{
m_p = new A;
}
A* B::get() const
{
//This is compiling fine
return m_p;
}
class C
{
public:
A* get() const;
private:
A m_a;
};
A* C::get() const
{
//Compiler generates an error for this. Why?
return &m_a;
}
EDIT: The compiler error is : error C2440: 'return' : cannot convert from 'const class A *' to 'class A *' Conversion loses qualifiers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
函数签名中的
const
告诉编译器该对象的成员不能被修改。 然而,您返回一个指向成员的非 const 指针,从而违反了该承诺。在您的类
B
中,您不许诺/不许诺,因为您不返回指向成员的指针,而是返回它的副本(并且该成员恰好是一个指针)。const
in the function signature tells the compiler that the object's members may not be modified. Yet you return a non-const
pointer to a member, thus allowing a violation of that promise.In your class
B
, you make/break no promise since you don't return a pointer to a member, you return a copy of it (and the member happens to be a pointer).这是因为您从 const 函数返回一个指向成员的非常量指针。
第一部分有效,因为您返回成员指针的副本,因此这不会违反 get 函数的常量性:
但是下一位会生成编译错误(在 gcc 4 上) )
因为您的 const get 函数通过返回指向 m_a 的非常量指针来提供对 m_a 的非常量访问。
It's because you're returning a non-const pointer to a member from a const function.
The first part works because you're returning a copy of a member pointer, so this doesn't violate the const-ness of the get function:
But the next bit generates the compile error (on gcc 4)
Because your const get function is providing non-const acess to m_a by returning a non-const pointer to it.
因为返回的指针不是const。 将其更改为:
请注意,C::get() 现在返回一个指向 A 的 const 指针。
Because the returned pointer is not const. Change it to this:
Notice that C::get() now returns a const pointer to A.
标记为 const 的成员函数不能返回非常量引用或指向私有变量的指针。 如果编译器允许这样做,类之外的任何人都可以修改所述私有变量,并且函数上的 const 限定符将失去意义。
Member functions marked
const
cannot return a non-const reference or pointer to a private variable. If the compiler allowed this, anyone outside your class would be able to modify the said private variable and theconst
qualifier on the function would lose meaning.这个问题可以用一个更简单的例子来说明:
在
MyClass::get() const
中,value
的类型为const int
。 当您取消引用它时,您将得到const int *
。 该类型无法安全(隐式)转换为int *
。 要纠正您的问题,请让get()
返回const int *
。This problem can be illustrated with a simpler example:
In
MyClass::get() const
,value
has the typeconst int
. When you dereference it, you getconst int *
. That type cannot be safely (implicitly) casted toint *
. To correct your problem, haveget()
returnconst int *
.由于 get() 是 const 函数,因此编译器将其引用的所有成员变量视为 const。 当您获取此类成员的地址时,您将获得一个指向 const 的指针。 但你的函数返回一个非常量指针。 您需要将代码更改为
Because get() is a const function, the compiler treats all member variables it refers to as const. When you take the address of such a member, you get a pointer to const. But your function is returning a non-const pointer. You need to change your code to
基本上只需在前面添加一个 const,
然后如果你想访问它,基本上就这样做:
但是,对我来说,你的程序没有什么意义。
IMO,这样做是最有意义的:
这样你就不必进行返回 A 实例的“get”。
Basically just add a const in front,
Then if you want to access it, basically do:
However, your program makes very little sense, to me.
IMO, it would make most sense to do:
That way you don't have to make a "get" that returns the instance of A.