casting to structure

Igor Shirkalin via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jun 24 15:19:03 PDT 2017


On Saturday, 24 June 2017 at 21:41:22 UTC, Moritz Maxeiner wrote:
> Hi, unfortunately not:
> - Operator overloading is supported via member functions only 
> [1].
> - Corollary: You cannot overload operators for builtin types 
> (i.e. where the cast gets rewritten to `e.opOverloaded` where 
> `e` is a builtin type)
> - opCast needs to be defined for the type that you are casting 
> from [2], not the one you are casting to
> => You cannot overload opCast for casting from builtin types
> [...]
> You cannot. As any such cast operation would have to create a 
> new A object, anyway (and would as such be a wrapper around a 
> (possibly default) constructor), I suggest using elaborate 
> constructors:

Thank you for your detailed explanation!

> Word of warning, though: What you're doing is highly unsafe.

I guess it is unsafe, but core.sys.windows module works this way.

> That *is* how the Windows C API works AFAIK.

I have no choice except for using it as it is.

> Define a struct for HWND as shown above for A and use 
> constructors. Add an `alias this` to the wrapped pointer value.

That was the first thing I did. And it is led me to my question.

Thank you!






More information about the Digitalmars-d-learn mailing list