我需要修改我的C ++类成员的顺序。例如:
class B {
public:
int i;
int j;
int k;
...
};
变为
class B {
public:
int j;
int k;
int i;
...
};
问题是我的大型代码库中存在奇怪的代码,这些代码依赖于类成员的相对位置。例如,某些函数会假设成员j的地址小于成员k的地址。
是否有任何CASE工具可以帮助我识别读取类成员地址的任何代码?
答案 0 :(得分:3)
我不知道有任何工具可以解决你的问题,但是我会定义一个类,它支持int
类型的所有运算符,并且会对符号运算符进行重载,以便运算符的结果不能转换为指针。然后我会在类成员定义中使用此类而不是int
,并查看编译器出错的位置。
像
这样的东西class IntWrapper {
public:
IntWrapper() { }
IntWrapper(const int x) { } // don't care about implementation as we
operator int() const { return 0; } // want compile-time errors only
IntWrapper& operator ++() { return *this; }
IntWrapper& operator ++(int) { return *this; }
...
void operator &() const { } // make it void so it would cause compiler error
};
然后:
class B {
public:
IntWrapper i;
IntWrapper j;
IntWrapper k;
...
};
这对使用boost::addressof
函数或引用的一些脏reinterpret_cast
没有帮助,但addressof
可能永远不会在您的项目中使用,以及{{1诀窍(谁会将它用于普通整数?)。
您还应该关注获取reinterpret_cast<char&>
类的整个对象的地址。