I seem to get a compile error with the below code using g++ 3.2.3:
g++ test.cpp
test.cpp: In function `int main()':
test.cpp:30: uninitialized const `cc
However, if I remove the comments around the default constructor (which should be the same as the one generated by the compiler, afaik), the code compiles and works as expected. Anyone know why this might be?
[8.5]
9. If no initializer is specified for an object, and the object is of
(possibly cv-qualified) non-POD class type (or array thereof), the object
shall be default-initialized; if the object is of const-qualified type,
the underlying class type shall have a user-declared default constructor.
Otherwise, if no initializer is specified for an object, the object and
its subobjects, if any, have an indeterminate initial value 90) ; if the
object or any of its subobjects are of const-qualified type, the program
is ill-formed.
$ g++ --version
g++ (GCC) 3.2.3
Copyright (C) 2002 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$
@Rollie;
Hi,
Rule for constant >> because we cannot subsequently change the value of an object declared to be const, we must initialize it when it is defined.
myClass doesn't have any default constructor to initialize a variable. So cc is uninitialized which is error in the case of constant variable.