Object construction and this

Benjamin Thaut code at benjamin-thaut.de
Sun Nov 10 08:22:24 PST 2013


Am 10.11.2013 17:06, schrieb Alexandr Druzhinin:
> 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?

Because in D Classes are reference types. That means the code you wrote 
is equivalent to the following C++ source:

class Foo
{
public:
   Foo(Object* o)
   {
     printf("%x\n", &this); // print a Foo**
   }
}

class Bar
{
public:
   Bar()
   {
     printf("%x\n", &this); // print a Bar**
   }
}

void main()
{
   Object* o = new Object;
   Foo* foo = new Foo(o);
   printf("%x\n", &foo); // print a Foo**

   Bar* bar = new Bar();
   printf("%x\n", &bar); // print a Bar**
}

My best guess would be that you wanted to pass "this" instead of "&this" 
to writeln as weel as "bar" instead of "&bar" etc.

Kind Regards
Benjamin Thaut


More information about the Digitalmars-d-learn mailing list