Topic: Initialisation of const objects
Author: comeau@panix.com (Greg Comeau)
Date: 2000/10/10 Raw View
In article <39e07d61.15083146@news.nikoma.de>,
Martin Aupperle <MikeAlpha@NoSpam_csi.com> wrote:
>I have a POD like
>
> struct S { int x; int y; };
>
>I can define a const Objekt like
>
> const S s;
You can't.
>and not explicitely initialize it.
You still can't. :)
>Formaly it is initialized by the
>Default-Standard-Constructor which is automatically generated, but is
>empty. The net effect is that we have a completely uninitialized
>constant object!
Not possible.
>The language does not allow uninitialized constants of fundamental or
>array types. Why does it allow that for structure types?
It doesn't. The best you can get is:
extern const S s;
- Greg
--
Comeau Computing / Comeau C/C++ "so close" 4.2.44 betas NOW AVAILABLE
TRY Comeau C++ ONLINE at http://www.comeaucomputing.com/tryitout
Email: comeau@comeaucomputing.com / WEB: http://www.comeaucomputing.com
---
[ 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://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: MikeAlpha@NoSpam_csi.com (Martin Aupperle)
Date: 2000/10/09 Raw View
Hello,
I have a POD like
struct S { int x; int y; };
I can define a const Objekt like
const S s;
and not explicitely initialize it. Formaly it is initialized by the
Default-Standard-Constructor which is automatically generated, but is
empty. The net effect is that we have a completely uninitialized
constant object!
The language does not allow uninitialized constants of fundamental or
array types. Why does it allow that for structure types? I try to
understand the reasoning behind. D&E does not say anything here.
Another question is why we can have partially initialized constants of
PODs but not of arrays:
const S s2 = { 1 }; // OK - s2.y will get 0
const int ai[5] = { 1 }; // NOT OK - ai[1]..ai[4] will NOT GET
0
Thanks - Martin.
------------------------------------------------
Martin Aupperle
MikeAlpha@NoSpam_csi.com
(remove NoSpam_)
------------------------------------------------
---
[ 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://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: Pete Becker <petebecker@acm.org>
Date: 2000/10/09 Raw View
Martin Aupperle wrote:
>
> Hello,
> I have a POD like
>
> struct S { int x; int y; };
>
> I can define a const Objekt like
>
> const S s;
>
> and not explicitely initialize it. Formaly it is initialized by the
> Default-Standard-Constructor which is automatically generated, but is
> empty. The net effect is that we have a completely uninitialized
> constant object!
No, we have a constant object that has been innitialized by the default
constructor for its type. If you don't like what the compiler-generated
default constructor does you can write your own.
--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Contributing Editor, C/C++ Users Journal (http://www.cuj.com)
---
[ 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://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: wmm@fastdial.net
Date: 2000/10/09 Raw View
In article <39e07d61.15083146@news.nikoma.de>,
MikeAlpha@NoSpam_csi.com (Martin Aupperle) wrote:
> Hello,
> I have a POD like
>
> struct S { int x; int y; };
>
> I can define a const Objekt like
>
> const S s;
>
> and not explicitely initialize it. Formaly it is initialized by the
> Default-Standard-Constructor which is automatically generated, but is
> empty. The net effect is that we have a completely uninitialized
> constant object!
>
> The language does not allow uninitialized constants of fundamental or
> array types. Why does it allow that for structure types? I try to
> understand the reasoning behind. D&E does not say anything here.
No, the Standard does not allow uninitialized structure types
of this kind. You didn't say what scope the declaration of "s"
is in. It makes a difference.
If "s" is in namespace scope, the members are zero-initialized
(8.5p6). If "s" has automatic storage duration, the declaration
is ill-formed (8.5p9 says that a const object must have either a
user-declared default constructor or have an initializer).
You can still get uninitialized members, of course, by writing a
constructor that doesn't initialize some or all of the class's
members, but that's not the situation you described.
> Another question is why we can have partially initialized constants of
> PODs but not of arrays:
>
> const S s2 = { 1 }; // OK - s2.y will get 0
>
> const int ai[5] = { 1 }; // NOT OK - ai[1]..ai[4] will NOT GET
> 0
What leads you to think this is the case? 8.5.1 treats array
and class aggregates the same, as far as I can tell.
--
William M. Miller, wmm@fastdial.net
Vignette Corporation (www.vignette.com)
Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ 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://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]