www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Object construction and this

reply Alexandr Druzhinin <drug2004 bk.ru> writes:
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?
Nov 10 2013
next sibling parent reply Benjamin Thaut <code benjamin-thaut.de> writes:
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
Nov 10 2013
parent Alexandr Druzhinin <drug2004 bk.ru> writes:
10.11.2013 23:22, Benjamin Thaut пишет:
 Am 10.11.2013 17:06, schrieb Alexandr Druzhinin:

 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
Yes, of course I should pass this instead of &this, thanks. I was looking for bug in wrong place - and using & let me hope I found it. :) But kind people didn't let me get up on the wrong way. Thanks!
Nov 10 2013
prev sibling parent "anonymous" <anonymous example.com> writes:
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
Nov 10 2013