Invoking a nonconst method on a member from a const method(从 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.
这篇关于从 const 方法对成员调用非 const 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 const 方法对成员调用非 const 方法
- DoEvents 等效于 C++? 2021-01-01
- OpenGL 对象的 RAII 包装器 2021-01-01
- 使用 __stdcall & 调用 DLLVS2013 中的 GetProcAddress() 2021-01-01
- 哪个更快:if (bool) 或 if(int)? 2022-01-01
- XML Schema 到 C++ 类 2022-01-01
- 如何提取 __VA_ARGS__? 2022-01-01
- GDB 不显示函数名 2022-01-01
- 从父 CMakeLists.txt 覆盖 CMake 中的默认选项(...)值 2021-01-01
- 将 hdc 内容复制到位图 2022-09-04
- 将函数的返回值分配给引用 C++? 2022-01-01
