On Wednesday, 22 November 2017 at 15:36:22 UTC, Adam D. Ruppe wrote:
On Wednesday, 22 November 2017 at 15:31:36 UTC, Tim Hsu wrote:
It seems in D, reference has its own address, am I right? unlike c++

The local variable does have its own address. Do not take its address - avoid &this or &object.

Just cast the ref itself.


In D, a class this or Object variable is already like a C++ Foo*. If you & that, you get a Foo** - not what you want in most cases.

Thanks after doing some experiment I guess I finally understand
class App {
    @property void *ptr() {
        return cast(void *)(this);
    }
}
void printaddr(void *ptr)
{
  writeln(ptr);
}
void main()
{
    Pizza pza = new Pizza("XD");
    Pizza pza2 = pza;
    printaddr(pza.ptr);
    printaddr(pza2.ptr);
    printaddr(&pza);
    printaddr(&pza2);
}

Result:
A32000
A32000
19FDB0
19FDB4


Conclusion:
pza and pza2 is two different reference variable refer to same new-ed object.

Reply via email to