Passing this to void *

Adam D. Ruppe destructionator at gmail.com
Wed Nov 22 15:34:53 UTC 2017


On Wednesday, 22 November 2017 at 15:27:27 UTC, Dukc wrote:
> It's worth noting that you will still be passing different 
> addresses to foo(void*) because classes are reference types in 
> D (structs are not). In the constructor you're passing the 
> address of the class object itself, but in the main function 
> you're passing the address of the reference.

No, in both cases, if you do as I say, you will be passing the 
same address.

You cast the reference itself. Do NOT use the & operator at any 
point with class objects in D (unless you legit know what you are 
doing). Just plain cast it:

`cast(void*) this` // ok. gets address of class object itself
`Object o; foo(cast(void*) o);` // also ok, does same thing

`cast(void*) &this` // NOT OK! DO NOT DO THIS address of a local
`cast(void*) &o`    // NOT OK! DO NOT DO THIS address of a local


More information about the Digitalmars-d-learn mailing list