WeakRefs for a CPP->D wrapper

Abdulhaq alynch4047 at gmail.com
Sat Jan 11 12:38:31 PST 2014


On Saturday, 11 January 2014 at 20:17:14 UTC, Tobias Pankrath 
wrote:
>
> class X {};
> X x;
>
> x is an reference to an instance of X, with other words a 
> pointer without arithmetic but with syntax sugar. &x will take 
> the address of this pointer/reference. If you want the address 
> of the actual instance, you can use cast(void*) for example.

Hi Tobias, can casting the address to void* make a difference to 
its value?

Here's an example of what I don't understand:

import std.stdio;
import std.string: format;

class Foo {
	int x;
}

void printAddress(Foo foo) {
	writeln("Address of parameter foo is %x".format(&foo));
	writeln("Address of parameter foo cast to void* is 
%x".format(cast(void*) &foo));
}

void main() {
	auto foo = new Foo();
	writeln("Address of foo is %x".format(&foo));
	writeln("Address of foo cast to void* is %x".format(cast(void*) 
&foo));
	printAddress(foo);
}

When run I get:

Address of foo is 7fff40ac4558
Address of foo cast to void* is 7fff40ac4558
Address of parameter foo is 7fff40ac4538
Address of parameter foo cast to void* is 7fff40ac4538

So why is the address of the parameter foo different to the 
address of main foo, when they both refer to the same object?

thanks



More information about the Digitalmars-d-learn mailing list