How to get the pointer of "this" ?

Vinod K Chandran kcvinu82 at gmail.com
Tue May 26 11:44:58 UTC 2020


On Monday, 25 May 2020 at 16:39:30 UTC, Mike Parker wrote:
> On Monday, 25 May 2020 at 08:39:23 UTC, John Burton wrote:
>
>> I believe that in D *this* is a reference to the
>> object and not a pointer like in C++.
>> So I think that writing &this might be what you need?
>
> No. A class reference is a pointer under the hood. Getting its 
> address will result in a pointer to the reference variable 
> itself, not to the class instance. When passing a reference to 
> a C API, casting it directly to the C type is correct.

Try this code. This will reproduce the same error.
import std.stdio : log = writeln;
void main() {
      log("Let's check whether 'this' is an lvalue or not.");
      Button btn = new Button("A button");
}

class Button {
     this(string btntext)    {
         mtext = btntext;
         log("button created with the name , ", btntext);
         log(&this);
     }
     private:
     string mt
}


More information about the Digitalmars-d-learn mailing list