Replacement for C++ Style Implicit casts?

Mike Chaten mchaten at gmail.com
Mon Oct 18 13:03:35 PDT 2010


I was under the impression that alias this just was shorthand for
Class Foo {
int x;
alias x this;
}
Foo foo =  new Foo
foo = 9; // foo.x = 9
Foo Foo = 9 // null.x =9;

Also, for opCast, doesnt that only work for going from Foo to int and not
the other way around?

-Mike
On Oct 18, 2010 3:55 PM, "Steven Schveighoffer" <schveiguy at yahoo.com> wrote:
> On Mon, 18 Oct 2010 15:47:40 -0400, Mike Chaten <mchaten at gmail.com> wrote:
>
>> In C++ it is possible to declare a class as follows
>> class Foo {
>> Foo(int x) { }
>> }
>> You can then use that constructor to implicitly convert int to Foo. E.g
>> Foo x = 0; //equivalent to Foo(0)
>>
>> Is there a way in D to do an implicit or explicit conversion from an
>> integral type to a class?
>
> explicit cast == opCast
> implicit cast == alias this
>
> Look up those two features in the docs.
>
> -Steve
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20101018/6ad285f5/attachment.html>


More information about the Digitalmars-d-learn mailing list