考虑以下C++ 03程序:
#include <iostream>
struct T
{
mutable int x;
T() : x(0) {}
};
void bar(int& x)
{
x = 42;
}
void foo(const T& t)
{
bar(const_cast<int&>(t.x));
}
int main()
{
T t;
foo(t);
std::cout << t.x << '\n';
}
Run Code Online (Sandbox Code Playgroud)
它似乎有用,但肯定是安全的吗?
我只修改了一个mutable字段,但const完全剥离了它的上下文让我感到紧张.
| 归档时间: |
|
| 查看次数: |
189 次 |
| 最近记录: |