任何人都可以解释这个表达的含义吗?

de *_*sto 0 c++ types type-conversion

 void* GetData()
 {
    return reinterpret_cast<unsigned char*>(this);
 }
Run Code Online (Sandbox Code Playgroud)

在这种情况下是否会发生自动类型强制的情况?我怎么能将我的类的对象转换为unsigned char*?

wil*_*ell 6

我假设你参考 return reinterpret_cast<unsigned char*>(this);

这是一个令人讨厌的块,可能采取错误的方法来解决错误的问题.从技术上讲,它要求编译器将this指针的类型重新解释为unsigned char*指针.这里没有做任何工作:该语句仅指示编译器将指针视为不同类型.如果将类型解释为错误,则这是灾难性的.

如果你想按位检查你的对象,那么你可能会尝试这样的事情:

union inspect_klass {
    klass obj;
    char bits[sizeof(klass)];
};
Run Code Online (Sandbox Code Playgroud)

编辑:我对上述联盟的信心现在已经动摇了.不要使用它,我正在寻找确认这个技巧确实被打破的确认.:■

  • 但是,通过联合检查一个类是不明确的行为:http://stackoverflow.com/questions/2310483/purpose-of-unions-in-c-and-c (3认同)