Transparent cast from class to member pointer?

Robert M. Münch robert.muench at saphirion.com
Mon Apr 15 15:07:10 UTC 2019


On 2019-04-15 08:19:57 +0000, Ali ‡ehreli said:

> 'alias this' can do that:

Hi, I had the suspicion already...

> struct IM;
> struct C {
>   IM *impl;
> };
> 
> int cInit(C* self) {
>    return 0;
> }
> 
> class I {
>      C handler;
> 
>      this(){cInit(&handler);}
> 
>    C* ptr() {               // <== ADDED
>      return &handler;
>    }
> 
>    alias ptr this;          // <== ADDED
> }
> 
> void someFunc(C* self) {
> }
> 
> void main() {
>    I myI = new I();
>    someFunc(myI);
> }

Bingo, I didn't know that I can do an 'alias this' using a function and 
not only a type... pretty cool. So, with several of these I can setup 
implicit conversions to different types. Thanks.

-- 
Robert M. Münch
http://www.saphirion.com
smarter | better | faster



More information about the Digitalmars-d-learn mailing list