Object construction and this

anonymous anonymous at example.com
Sun Nov 10 08:23:30 PST 2013


On Sunday, 10 November 2013 at 16:06:40 UTC, Alexandr Druzhinin
wrote:
> Could somebody explain why this http://dpaste.dzfl.pl/248262b9 
> return this:
> Foo:
> 7FBFD59A50
> 7FBFD59A80
> Bar:
> 7FBFD59A58
> 7FBFD59A88
> Why this value in ctor is different from this value out of ctor 
> if ctor gets an argument?

The "bar" values are different, too. And they are, because you're
printing the addresses of the references (variables), not the
addresses of the objects. You can use cast(void*) to get the
address of an object:

class Bar
{
	this()
	{
		writeln(cast(void*) this);
	}
}
void main()
{
	writeln("Bar:");
	auto bar = new Bar();
	writeln(cast(void*) bar);
}
---
Bar:
7FDBD5BFEFF0
7FDBD5BFEFF0


More information about the Digitalmars-d-learn mailing list