Tag: offsetof

为什么不能在C ++的非POD结构上使用offsetof?

我正在研究如何在C ++中获得成员的内存偏移量,并在wikipedia中遇到这个问题: 在C ++代码中,您不能使用offsetof来访问不是普通旧数据结构的结构或类的成员。 我试了一下,似乎工作正常。 class Foo { private: int z; int func() {cout << "this is just filler" << endl; return 0;} public: int x; int y; Foo* f; bool returnTrue() { return false; } }; int main() { cout << offsetof(Foo, x) << " " << offsetof(Foo, y) << " " << offsetof(Foo, […]