Topic: simple constructors : default value


Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1998/02/07
Raw View
Loic Tregan wrote:

> would the following extension be useful ?
[...]
> the rule : when a field is not in explicited initialised in a ctor, use
> its default initialisation.
>
> I think the constructor would be both ' lightest' (you write only the
> specific initialisations) and more reliable ( if you want to change a
> initialisation, modify just the default value and don't parse all the
> constructors, with the risk to forget one).

You can factor out the common innitialisation in a private
init function.

You can't do that for const members but writting

class X {
    const int i = 4;

is non-sens anyway.

I know it isn't ideal in all cases, but it's an acceptable
tradeof in all the cases I have meet to far.

So there is no real need for an extension.

--

Valentin Bonnard                mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://www.pratique.fr/~bonnardv/


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Loic Tregan <Loic.Tregan@cert.fr>
Date: 1998/02/09
Raw View
Valentin Bonnard wrote:
>
> Loic Tregan wrote:
>
> > would the following extension be useful ?
> [...]
> > the rule : when a field is not in explicited initialised in a ctor, use
> > its default initialisation.
> >
> > I think the constructor would be both ' lightest' (you write only the
> > specific initialisations) and more reliable ( if you want to change a
> > initialisation, modify just the default value and don't parse all the
> > constructors, with the risk to forget one).
>
> You can factor out the common innitialisation in a private
> init function.
>

 no, because the members are temporaly initialized by their default
constructors.
  second, it is more easy to write ' int  x=3' rather than to write this
init method. I think that small changes likes this one could improve the
productivity of C++ programmers and make the language more accessible
for people.


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Loic Tregan <Loic.Tregan@cert.fr>
Date: 1998/02/04
Raw View
would the following extension be useful ?

class X {
 int O = 5;  // extension
 A  a(7);    // extension
        X();
        X( int o) : O(o) {}
        X( int stuff ) : a(stuff) {}
        X( int o, int stuff ) : A(stuff), O(o) {}
};

with the following semantics :

class X {
 int O;     // standard C++
 A  a;      // standard C++
        X() : O(5), a(7) {};
        X( int o) : O(o), a(7) {}
        X( int stuff ) : a(stuff), O(5) {}
        X( int o, int stuff ) : A(stuff), O(o) {}
};

the rule : when a field is not in explicited initialised in a ctor, use
its default initialisation.

I think the constructor would be both ' lightest' (you write only the
specific initialisations) and more reliable ( if you want to change a
initialisation, modify just the default value and don't parse all the
constructors, with the risk to forget one).
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]