Passing this to void *
Steven Schveighoffer
schveiguy at yahoo.com
Fri Nov 24 16:08:59 UTC 2017
On 11/23/17 12:57 AM, Nicholas Wilson wrote:
> On Wednesday, 22 November 2017 at 15:07:08 UTC, Tim Hsu wrote:
>> I am a C++ game developer and I want to give it a try.
>>
>> It seems "this" in Dlang is a reference instead of pointer.
>>
>> How can I pass it as void *?
>>
>> void foo(void *);
>>
>> class Pizza {
>> public:
>> this() {
>> Pizza newone = this;
>> // works but newone is actually not this pizza.
>> foo(&newone);
>> // this does not work..
>> foo(this);
>> }
>> }
>>
>> void main() {
>> Pizza pizza = new Pizza();
>> // this works...
>> foo(&pizza);
>> }
>
> Note that all the examples and advice in this thread apply to _classes_,
> not to structs.
A further way to look at it, a class is really not the same as what you
normally think as references (either ref parameter in D or & type
constructor in C++). It's really simply a pointer that cannot be used
with pointer math.
ref parameters:
* cannot be rebound (you can't make it point at another value)
* don't act any differently than auto-storage variables. Making a copy
does not make a new reference, but rather copies the whole thing.
class references:
* CAN be rebound.
* copying to another variable does not copy the data, ever (you can't
make a variable that represents the class data)
-Steve
More information about the Digitalmars-d-learn
mailing list