Topic: prohibit implicit initialization (was: new class initializer syntax?)
Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1997/02/03 Raw View
--MimeMultipartBoundary
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Thomas A. Horsley wrote:
>
> >Now: What should the copy constructor do with the omitted count?
>
> I'm not sure how those examples are relevant to default initializers. I
> can screw things up exactly the same way with or without a default
> initializer :-). The answer in my proposal is:
>
> >2. Initialize with 0 (like the other constructors do)
> ...
> >while 2 would easily lead to nasty bugs as soon as someone gets used to
> >omit initializers on constructors
>
> The problem is that initializers *already* get omitted quite frequently.
> Just because it is possible to contrive an example in which the default
> initializer doesn't help doesn't mean there aren't examples where it does
> help.
>
> Actually, as an alternate proposal, I'd be perfectly happy if the language
> simply made it an error to provide a constructor which does not *explicitly*
> initialize each member (as long as there was a way to say "this member is
> explicitly NOT initialized"). But I bet such a change would upset a tad
> more people that providing a default initializer would :-). (Hmmm... maybe
> we can add a compiler option to do this...).
What about reusing the keyword "explicit" to define member variables
which must be initialized explicitly? Like this:
class MyClass
{
int x; // normal member
explicit int y; // explicit member
public:
MyClass(): x(1) {} // error: y not initialized
MyClass(int i): y(i) {} // ok: x is implicitly "initialized"
MyClass(char j): x(j), y(j+1) {} // ok
MyClass(OtherClass&) {} // error: y not initialized
};
This way you would get the desired behaviour by declaring all
data members explicit. OTOH it would not break any old code,
and it would be a small change (one addition to declaration
syntax, such that member data declarations in classes may be preceded
by explicit, and one concerning the initializer list, such that
data members declared explicit may not be omitted. No other changes
would be required.)
Maybe this should be extended to base classes, for completeness:
class Base1 {};
class Base2 {};
class Derived: public Base1, public explicit Base2
{
public:
Derived() {} // error: Base2 not initialized
Derived(int): Base2() {} // ok: Base2 explicitly initialized
};
But this would probably much less useful, as in this case you can
get what you want already today by simply giving no default constructors
for the base classes (or making them private if no other
constructor is given).
--MimeMultipartBoundary--
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]