Suggestion: shortcut for 'new X'

Kristian kjkilpi at gmail.com
Thu Aug 24 09:49:36 PDT 2006


On Thu, 24 Aug 2006 19:06:29 +0300, freeagle <freeagle at inmail.sk> wrote:

> Kristian wrote:
>> On Thu, 24 Aug 2006 18:10:12 +0300, Oskar Linde  
>> <oskar.lindeREM at OVEgmail.com> wrote:
>>
>>> Kristian wrote:
>>>> How about this:
>>>>      Obj obj1 = new Obj;
>>>>     Obj obj2 = new Obj(10);
>>>>  could be reduced to:
>>>>      Obj obj1 = new;
>>>>     Obj obj2 = new(10);
>>>>  This way only one class name would be required per variable. (That  
>>>> is, one at maximum: "Obj obj1 = new, obj2 = new(10);" is also valid  
>>>> of course.) Redundance would be reduced.
>>>
>>> You can already do:
>>>
>>> auto obj1 = new Obj;
>>> auto obj2 = new Obj(10);
>>>
>>> which takes care of the redundant Obj.
>>  Yes, and it's nice.
>> But you cannot always use auto.
>>  Obj func() {
>>     Obj ret = new;
>>      ret.doSomething();
>>     ...
>>     return(ret);
>> }
>>  That would be nice too. :)
>
> And if you want to create object of inherited class and store it into  
> referenco to base class??
>
> class A
> {
> }
>
> class B : A
> {
> }
>
> A a = new/* B */; ??

If you write "A a = new;" it would equal to "A a = new A;"

If you would like to create an object of type B, then you have to use the  
longer (normal) version:

A a = new B;

There is no redundancy in "A a = new;" and "A a = new B;". However, there  
is redundancy in "A a = new A;".



More information about the Digitalmars-d mailing list