Replacement for C++ Style Implicit casts?

Denis Koroskin 2korden at gmail.com
Mon Oct 18 13:18:52 PDT 2010


On Mon, 18 Oct 2010 23: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?
>
> -Mike

For structs the following works:

struct Foo
{
	this(int x) { ... }
}

Foo x = 0;

Classes are allocated on heap and as such there is no way to achieve the  
same.


More information about the Digitalmars-d-learn mailing list