【发布时间】:2015-03-13 18:38:54
【问题描述】:
有时,在传递或返回结构时,可能需要将某些字段设为 const:
struct A
{
char c;
int x;
};
struct B
{
const char c;
int x;
};
void process(B& b)
{
if(b.c=='1')
b.x++;
}
void test()
{
A a;
a.c = '1';
a.x = 0;
process(reinterpret_cast<B&>(a));
}
这种部分 const 类型转换是否足够便携和安全?
【问题讨论】:
-
我们可以做一些类似
process(reinterpret_cast<B&>(const_cast<A&>(a)));的事情,它会起作用看到这个ideone.com/xnblvi
标签: c++ struct casting constants partial