 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Marlene Miller Guest
|
Posted: Wed Jul 28, 2004 10:10 pm Post subject: const B b; must be initialized |
|
|
Why must b be initialized? Why isn't the implicit default constructor
sufficient?
class A {};
class B {
private:
A a;
};
class C {
public:
C () {}
private:
A a;
};
void f() {
const B b; //error: Constant variable 'b' must be initialized
const B b2 = B(); // ok
const C c; // ok
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dag Viken Guest
|
Posted: Sat Jul 31, 2004 3:06 am Post subject: Re: const B b; must be initialized |
|
|
I think it is because your B class is using a const default constructor,
which means that its data is placed in a constant data segment. The
constructor would therefore have to be evaluated at compile time, together
with the default A class constructor. The other two examples are evaluated
at runtime: const B b2=B(); const C c;
Lose the const or lose the A member variable and you are OK.
"Marlene Miller" <marlenemiller (AT) worldnet (DOT) att.net> wrote
| Quote: | Why must b be initialized? Why isn't the implicit default constructor
sufficient?
class A {};
class B {
private:
A a;
};
class C {
public:
C () {}
private:
A a;
};
void f() {
const B b; //error: Constant variable 'b' must be initialized
const B b2 = B(); // ok
const C c; // ok
}
|
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Marlene Miller Guest
|
Posted: Sat Jul 31, 2004 1:50 pm Post subject: Re: const B b; must be initialized |
|
|
"Dag Viken" <dag.viken (AT) earthlink (DOT) net> wrote
| Quote: | I think it is because your B class is using a const default constructor,
which means that its data is placed in a constant data segment. The
constructor would therefore have to be evaluated at compile time, together
with the default A class constructor. The other two examples are evaluated
at runtime: const B b2=B(); const C c;
Lose the const or lose the A member variable and you are OK.
|
Thank you Dag. Thank you for point of view, and for taking the time to think
about my question and respond.
Ok. When there is no explicit constructor, the object is allocated and
initialized at compile time. A true constant.
In the C++ Programming Language, 3rd ed., 5.4, "Note that const modifies a
type; this is, it restricts the ways in which an object can be used, rather
than specifying how the constant is to be allocated."
I would be inclined to say this quote disagrees with the compiler.
Marlene
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
John Potter Guest
|
Posted: Sat Jul 31, 2004 6:48 pm Post subject: Re: const B b; must be initialized |
|
|
On 28 Jul 2004 18:10:52 -0400, "Marlene Miller"
<marlenemiller (AT) worldnet (DOT) att.net> wrote:
| Quote: | Why must b be initialized? Why isn't the implicit default constructor
sufficient?
|
Because C++ has a standard.
This class just adds confusion.
| Quote: | class B {
private:
|
The private part makes this a non-POD.
It is still an error if A is changed to int.
| Quote: | };
void f() {
const B b; //error: Constant variable 'b' must be initialized
|
This is default initialization of a non-POD.
8.5/9 ... if the object is of const-qualified type, the underlining
class type shall have a user declared default constructor.
It doesn't have one.
If you remove the private and change class to struct, it will be
a POD. 8.5/9 then states that the uninitialized const POD is
ill-formed.
Just the facts. Direct why questions elsewhere.
John
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dag Viken Guest
|
Posted: Sun Aug 01, 2004 2:17 am Post subject: Re: const B b; must be initialized |
|
|
Thank you for your appreciation.
Unfortunately, I only have the 2nd edition of the C++ Programming Language,
but I think that the compiler is not always right. It is just an instance of
some clever thinking.
Dag
"Marlene Miller" <marlenemiller (AT) worldnet (DOT) att.net> wrote
| Quote: |
"Dag Viken" <dag.viken (AT) earthlink (DOT) net> wrote in message
news:s%tOc.3131$cK.2234 (AT) newsread2 (DOT) news.pas.earthlink.net...
I think it is because your B class is using a const default
constructor,
which means that its data is placed in a constant data segment. The
constructor would therefore have to be evaluated at compile time,
together
with the default A class constructor. The other two examples are
evaluated
at runtime: const B b2=B(); const C c;
Lose the const or lose the A member variable and you are OK.
Thank you Dag. Thank you for point of view, and for taking the time to
think
about my question and respond.
Ok. When there is no explicit constructor, the object is allocated and
initialized at compile time. A true constant.
In the C++ Programming Language, 3rd ed., 5.4, "Note that const modifies a
type; this is, it restricts the ways in which an object can be used,
rather
than specifying how the constant is to be allocated."
I would be inclined to say this quote disagrees with the compiler.
Marlene
|
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Marlene Miller Guest
|
Posted: Sun Aug 01, 2004 11:09 am Post subject: Re: const B b; must be initialized |
|
|
| Quote: | Why must b be initialized? Why isn't the implicit default constructor
sufficient?
Because C++ has a standard.
|
Okay. It's time for me to get the Standard. I've been relying on
Stroustrup's The C++ Programming Language.
| Quote: | class A {};
This class just adds confusion.
|
Good observation.
| Quote: | class B {
private:
The private part makes this a non-POD.
|
I don't know what a POD is.
| Quote: | A a;
It is still an error if A is changed to int.
|
Good idea. It's better to use an int.
| Quote: | void f() {
const B b; //error: Constant variable 'b' must be initialized
This is default initialization of a non-POD.
8.5/9 ... if the object is of const-qualified type, the underlining
class type shall have a user declared default constructor.
|
Okay. Thank you for finding the relevant rule.
| Quote: |
It doesn't have one.
If you remove the private and change class to struct, it will be
a POD. 8.5/9 then states that the uninitialized const POD is
ill-formed.
Just the facts. Direct why questions elsewhere.
|
Thank you very much. I do wish I understood why.
Marlene
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Sun Aug 01, 2004 2:19 pm Post subject: Re: const B b; must be initialized |
|
|
In article <v_TOc.4551$9Y6.904 (AT) newsread1 (DOT) news.pas.earthlink.net>, Dag
Viken <dag.viken (AT) earthlink (DOT) net> writes
| Quote: | Thank you for your appreciation.
Unfortunately, I only have the 2nd edition of the C++ Programming Language,
but I think that the compiler is not always right. It is just an instance of
some clever thinking.
|
In regards to Standard C++ the second edition of TC++PL is not always
right, and current compilers, though far from perfect, are more likely
to be right. A great deal was added and modified in C++ between the 2nd
and 3rd editions of TC++PL.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Maxim Yegorushkin Guest
|
Posted: Mon Aug 02, 2004 10:30 am Post subject: Re: const B b; must be initialized |
|
|
Marlene Miller <marlenemiller (AT) worldnet (DOT) att.net> wrote:
| Quote: | I don't know what a POD is.
|
There is a clear explanation from Comeau guys: http://www.comeaucomputing.com/techtalk/#pod
--
Maxim Yegorushkin
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Allan W Guest
|
Posted: Tue Aug 03, 2004 11:24 am Post subject: Re: const B b; must be initialized |
|
|
John Potter <jpotter (AT) falcon (DOT) lhup.edu> wrote
| Quote: | 8.5/9 ... if the object is of const-qualified type, the underlining
class type shall have a user declared default constructor.
It doesn't have one.
If you remove the private and change class to struct, it will be
a POD. 8.5/9 then states that the uninitialized const POD is
ill-formed.
Just the facts. Direct why questions elsewhere.
|
It makes sense that all const objects have to be initialized somewhere,
because once it's constructed, you can never change the value. If you
don't have a constructor, then you're going to have to initialize it
some other way.
Think about creating a const int without initializing it:
int main() {
const int x; // HUH?
// Now, what can you DO with it?
std::cout << x << std::endl; // What value is printed?
}
This is almost certainly an error... x is mostly useless(*).
(*) I guess there are some conceivable uses where the value doesn't matter.
For instance, you know that the address of x is distinct from the address
of all other integers... not sure I want to go there... let me just
observe that if the value doesn't matter, then there's no reason not to
zero-initialize it.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dag Viken Guest
|
Posted: Wed Aug 04, 2004 9:58 am Post subject: Re: const B b; must be initialized |
|
|
"John Potter" <jpotter (AT) falcon (DOT) lhup.edu> wrote
| Quote: | On 28 Jul 2004 18:10:52 -0400, "Marlene Miller"
[email]marlenemiller (AT) worldnet (DOT) att.net[/email]> wrote:
Why must b be initialized? Why isn't the implicit default constructor
sufficient?
Because C++ has a standard.
class A {};
This class just adds confusion.
class B {
private:
The private part makes this a non-POD.
A a;
It is still an error if A is changed to int.
};
void f() {
const B b; //error: Constant variable 'b' must be initialized
This is default initialization of a non-POD.
8.5/9 ... if the object is of const-qualified type, the underlining
class type shall have a user declared default constructor.
It doesn't have one.
If you remove the private and change class to struct, it will be
a POD. 8.5/9 then states that the uninitialized const POD is
ill-formed.
|
As far as I know class members are private by default and struct members ar
public by default, and that is the only difference between classes and
structs in C++. A POD(Plain Old Data) data type is is a data type that
contains only the elementary data types: int, char, short, float, double,
enum, ..., and it does not initialize them. A class/struct has the ability
to use a constructor to initialize and set any or all variables in the
class/struct.
Problem is, POD members are not initialized.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
John Potter Guest
|
Posted: Wed Aug 04, 2004 12:37 pm Post subject: Re: const B b; must be initialized |
|
|
On 4 Aug 2004 05:58:51 -0400, "Dag Viken" <dag.viken (AT) earthlink (DOT) net>
wrote:
| Quote: | "John Potter" <jpotter (AT) falcon (DOT) lhup.edu> wrote in message
news:KUOOc.21201$iK.8198 (AT) newsread2 (DOT) news.atl.earthlink.net...
If you remove the private and change class to struct, it will be
a POD. 8.5/9 then states that the uninitialized const POD is
ill-formed.
As far as I know class members are private by default and struct members ar
public by default, and that is the only difference between classes and
structs in C++.
|
True.
| Quote: | A POD(Plain Old Data) data type is is a data type that
contains only the elementary data types: int, char, short, float, double,
enum, ..., and it does not initialize them.
|
A POD is an aggregate and aggregates do not have private parts.
Changing private to public and leaving it a class is another way to
change it to a POD.
| Quote: | A class/struct has the ability
to use a constructor to initialize and set any or all variables in the
class/struct.
Problem is, POD members are not initialized.
|
So what? The point is declaring a const object without an initializer.
For a non-POD, there must be a user declared default ctor or the program
is ill-formed. The implementation defined one will not suffice. For a
POD, the program is ill-formed.
If you had something to add or a question, I don't see it.
John
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|