从 const 方法对成员调用非 const 方法
我很惊讶在const"中发现了这个漏洞":
I was surprised to find this "hole" in "const"ness:
#include <stdio.h>
class A
{
int r ;
public:
A():r(0){}
void nonconst()
{
puts( "I am in ur nonconst method" ) ;
r++;
}
} ;
class B
{
A a ;
A* aPtr ;
public:
B(){ aPtr = new A() ; }
void go() const
{
//a.nonconst() ; // illegal
aPtr->nonconst() ; //legal
}
} ;
int main()
{
B b ;
b.go() ;
}
所以基本上从const
方法B::go()
,你可以调用non-const成员函数(恰当地命名为nonconst()
) 如果 A
类型的对象被指针引用.
So basically from const
method B::go()
, you can invoke the non-const member function (aptly named nonconst()
) if object of type A
is referenced by a pointer.
这是为什么呢?似乎是一个问题(它在我的代码中,我在其中找到它.)
Why is that? Seems like a problem (it kind of was in my code, where I found it.)
推荐答案
当B
类型的对象和对象都是const,那么它的所有成员都是const,也就是说它的两个成员都是,对于B::go()
的持续时间,有效
When and object of type B
is const, then all of its members are const, which means its two members are, for the duration of B::go()
, effectively
A const a;
A * const aPtr;
第一个是A
类型的常量对象,你只能在其上调用const成员函数.然而,第二个是指向非常量A
的常量指针.您不能在函数 B::go()
中合法地说 aPtr = <anything>
,因为这会修改 aPtr
,它是恒定的.
The first is a constant object of type A
, on which you can only call const member functions. The second, however, is a constant pointer to a non-constant A
. You could not legally say aPtr = <anything>
from within the function B::go()
, since that would modify aPtr
, which is constant.
指向常量 A
的指针将被声明为 A const* aPtr
或 const A* aPtr
,然后调用非常量 A::nonconst()
非法.
A pointer to a constant A
would be declared as A const* aPtr
or const A* aPtr
, which would then make calling the non-constant A::nonconst()
illegal.
相关文章