Thread overview | ||||||
---|---|---|---|---|---|---|
|
November 10, 2013 Object construction and this | ||||
---|---|---|---|---|
| ||||
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? |
November 10, 2013 Re: Object construction and this | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alexandr Druzhinin | 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
|
November 10, 2013 Re: Object construction and this | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alexandr Druzhinin | 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
|
November 10, 2013 Re: Object construction and this | ||||
---|---|---|---|---|
| ||||
Posted in reply to Benjamin Thaut | 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!
|
Copyright © 1999-2021 by the D Language Foundation