initializer list like in C++?
BCS
ao at pathlink.com
Tue Jul 24 11:57:42 PDT 2007
Reply to James,
> Jarrett Billingsley wrote:
>
>> "Hoenir" <mrmocool at gmx.de> wrote in message
>> news:f84rnt$1db0$1 at digitalmars.com...
>>
>>> Is there anything like the initializer list (C++) in D?
>>>
>>> class Test {
>>> int x;
>>> Test(int y):x(y){};
>>> }
>> No. See
>> http://www.digitalmars.com/d/archives/digitalmars/D/initialization_li
>> sts_55262.html for an explanation on why D doesn't have them.
>>
> I looked at that thread, but found only references to performance,
> which miss the main reason why these are present/useful in C++:
> semantics. There are various kinds of things in C++ that can be
> initialized but cannot be modified, because of references, const-ness,
> and classes with immutable value semantics.
>
> The occasional/slight performance benefit is definitely secondary to
> the ability to express meaning directly in the code.
>
> As D gains the ability to model such immutable members, it might
> benefit from finding (its own) solution to the same issue.
>
IIRC this works
const int i;
static this(){i=5;}
class C
{
const char c;
this(char c_){c=c_;}
}
More information about the Digitalmars-d-learn
mailing list