On 12/31/22 16:35, Paul wrote:

> Can I acquire the address of a class object,

Answering that question literally, yes, you can by casting the class variable to void*. But note: 'class object' means the instance of class in D.

> not a class variable (i.e.
> the instantiations of the class)

D terminology is different there: A class variable is a reference to the class object (that carries the member variables, etc. of a class instance.)

  auto c = new C();

'c' is the *variable*, providing access to the *object* in dynamic memory.

> but the object definition itself?

As H. S. Teoh answered, Python etc. can do that but not D's compilation model.

The following program tries to demonstrate that the members are offset two void* sizes further from the address of the object:

class C {
    int i;
}

void main() {
    auto c = new C();

    const likelyOffsetOfMembers = 2 * (void*).sizeof;

    // HERE:
    const objectAddrInDynamicMemory = cast(void*)c;

    assert(objectAddrInDynamicMemory + likelyOffsetOfMembers == &c.i);
}

If the class is defined as extern(C++), then you must replace 2 above with 1:

extern(C++) class C {
    int i;
}

Ali

Reply via email to