Connecting Tech Pros Worldwide Help | Site Map

Help. const object uninitialized problem.

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 04:52 AM
nan.li.g@gmail.com
Guest
 
Posts: n/a
Default Help. const object uninitialized problem.

I have this simple code below. When I compiled it, I got the following
error. But after I removed the comment marker(//), i.e. explicitly
defined a constructor, it becomes OK.

The compiler should generate a default constructor for me and the
default one shoule be no different than the one I specified. Why do I
have to specify one in this code?

class A
{
public:
//A() { }
};


int main()
{
const A a;
return 0;
}


[nan@athena test]$ g++ test19.cpp
test19.cpp: In function `int main()':
test19.cpp:10: error: uninitialized const `a'


Thank you very much.


  #2  
Old July 23rd, 2005, 04:52 AM
Alf P. Steinbach
Guest
 
Posts: n/a
Default Re: Help. const object uninitialized problem.

* nan.li.g@gmail.com:[color=blue]
>
> class A
> {
> public:
> //A() { }
> };
>
>
> int main()
> {
> const A a;
> return 0;
> }
>
> The compiler should generate a default constructor for me and the
> default one shoule be no different than the one I specified. Why do I
> have to specify one in this code?[/color]

Look at the error message which you have repeated as subject line.

It's not meaningful to have a constant without a specified value.

You're lucky: Visual C++ 7.1 erronously compiles the above without
flagging the error.

[color=blue]
> [nan@athena test]$ g++ test19.cpp
> test19.cpp: In function `int main()':
> test19.cpp:10: error: uninitialized const `a'[/color]

In the code as-is no value is provided for the constant, and
according to §8.5/9 the program is "ill-formed".

You can (1) initialize explicitly, e.g..

A const a = {};

(allowed only for aggregate type, which A is) or

A const a = A();

Or you can (2) define your own default constructor.

Any way you need to explicitly define the value of the constant.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
  #3  
Old July 23rd, 2005, 04:53 AM
nan.li.g@gmail.com
Guest
 
Posts: n/a
Default Re: Help. const object uninitialized problem.

Thank you. Your answer is very clear.

Alf P. Steinbach wrote:[color=blue]
> * nan.li.g@gmail.com:[color=green]
> >
> > class A
> > {
> > public:
> > //A() { }
> > };
> >
> >
> > int main()
> > {
> > const A a;
> > return 0;
> > }
> >
> > The compiler should generate a default constructor for me and the
> > default one shoule be no different than the one I specified. Why do I
> > have to specify one in this code?[/color]
>
> Look at the error message which you have repeated as subject line.
>
> It's not meaningful to have a constant without a specified value.
>
> You're lucky: Visual C++ 7.1 erronously compiles the above without
> flagging the error.
>
>[color=green]
> > [nan@athena test]$ g++ test19.cpp
> > test19.cpp: In function `int main()':
> > test19.cpp:10: error: uninitialized const `a'[/color]
>
> In the code as-is no value is provided for the constant, and
> according to §8.5/9 the program is "ill-formed".
>
> You can (1) initialize explicitly, e.g..
>
> A const a = {};
>
> (allowed only for aggregate type, which A is) or
>
> A const a = A();
>
> Or you can (2) define your own default constructor.
>
> Any way you need to explicitly define the value of the constant.
>
> --
> A: Because it messes up the order in which people normally read text.
> Q: Why is it such a bad thing?
> A: Top-posting.
> Q: What is the most annoying thing on usenet and in e-mail?[/color]

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.