From 2846679200096763377
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,75a9ad8b6ce88ef7
X-Google-Attributes: gidf78e5,public
From: stephen.clamage@Eng.Sun.COM (Steve Clamage)
Subject: new class initializer syntax?
Date: 1997/01/30
Message-ID: <199701292257.OAA08545@taumet.eng.sun.com>#1/1
X-Deja-AN: 213294073
references: <upvypkzc9.fsf@worldnet.att.net>
x-original-date: Wed, 29 Jan 1997 14:57:24 -0800
organization: -
x-auth: PGPMoose V1.1 PGP comp.std.c++
newsgroups: comp.std.c++
originator: austern@isolde.mti.sgi.com


In article fsf@worldnet.att.net, Tom.Horsley@worldnet.att.net (Thomas A. Horsley) writes:
>After tracking down a bug, which for the 40 bazillionth time that turned out
>to be a missing member variable initializer on 2 out of 3 constructors for a
>class definition, it suddenly hit me: I only declare the member variable in
>one place - why do I have to initialize it in every $#@!  constructor when I
>just want it to be initialized to zero in all (or most) of 'em anyway?
>
>Has the standard committee considered any kind of new syntax for something
>like a "default initializer", so I can give a member an initial value in the
>class declaration which will be used if I don't provide a different
>initializer in the constructor?

You don't need to ask for such a facility. It has always been the case
in C++ that if you don't provide a member-initializer in a constructor,
the data member gets its default initialization. For a class type,
the default initialization is the constructor that needs no arguments.
For simple types (int, pointer, etc) the default initialization is
no initialization, so you have to provide member initializers if you want 
initialization to take place.

Maybe you would like all members of simple type to be automatically
initialized with zero, as is the case for static objects. That would require
every member of every object of every class in every program to pay the
price of initialization, even when it wasn't wanted. I don't think that
is likely to be adopted. (That would not necessarily be a bad language
design, but it doesn't fit with the C++ idea of not having to pay for
features you don't want.)

If you want something like "initialize any uninitialized members in this
class with this value", I can't think of a way to specify the feature so
that it is generally useful. Don't forget you have to cover cases like
members which are themselves classes, and base classes at different
depths, and virtual base classes, and so on. I also don't see how to
specify something like that for any value other than zero, given that
a class often has members of radically different kinds of type.
---
Steve Clamage, stephen.clamage@eng.sun.com
---
[ 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 
]



