views:

141

answers:

2

Let's say I create 5 objects, all from the same class. Would the byte offset of the first object be 0? How would I find out the byte offset of the other objects?

A: 

"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.

Alex Martelli
You'd either have to reinterpret_cast to char* or add another static_cast through void*. That said, `i * sizeof thearray[0]` should work just as well since array elements must occupy contiguous storage.
James McNellis
A: 

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

printf("%p %p", (void *) &thingOne, (void *) &thingTwo);
egrunin
@James McNellis: Fixed.
egrunin