Transparent cast from class to member pointer?
Ali Çehreli
acehreli at yahoo.com
Mon Apr 15 08:19:57 UTC 2019
On 04/14/2019 11:03 AM, Robert M. Münch wrote:
> struct IM;
> struct C {
> IM *impl;
> };
>
> int cInit(C* self);
>
> class I {
> C handler;
>
> this(){cInit(&handler);}
> }
>
> Is there a simple way that I can use handler without the address-of
> operator and automatically get *impl?
>
> Something like:
>
> class I {
> C handler;
>
> this(){cInit(handler);}
> }
>
> And later can use I in a way like this without having to define/write
> explicit casts everywhere?
>
> someFunc(C* self);
>
> I myI;
> someFunc(myI);
>
>
'alias this' can do that:
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);
}
Ali
More information about the Digitalmars-d-learn
mailing list