开发者

How to find the byte offset of an object in C++?

Let's say I create 5 objects, all from the same class. Would the byte offset of t开发者_高级运维he first object be 0? How would I find out the byte offset of the other objects?


"Byte offset" from what? Are you creating an array of 5 such objects? In that case, sure, the byte offset of the first one (from the start of the array) is 0; as for other objects,

static_cast<char*>(&thearray[i]) - static_cast<char*>(&thearray[0])

is the byte offset of the ith one.


Regardless of what you mean by offset, you can always examine the address of the objects yourself:

printf("%p %p", (void *) &thingOne, (void *) &thingTwo);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜