Uninitialized constant objects

  • Thread starter =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=
  • Start date
?

=?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=

Hello.

Based on this simple test program:

class Test {};

int main()
{
Test t;
const Test u;
}

Why does the definition of 'u' fail? What if all that's wanted is to
have a default constructed Test object? Must one explicitly write so? E.g.:

const Test u = Test();

What's the rationale for such restriction? And, lastly, why does
defining a default constructor solve the issue? I.e.:

class Test
{
public:
Test() {}
};

int main()
{
Test t;
const Test u;
}

Now the compiler accepts it just fine. Why? Pardon me if the answer to
these questions should be obvious. I don't presently find them to be so.

Thank you,
 
B

bkirungein

which compiler you are using..?
I tried it on VC++ compiler it is not giving any error in both the
cases

since constructor is called during object creation, it doesn't matter
wether the object is const or not

by declaring a object as const the only restrictions that you have on
it is
-- you can only call const methods of that object( ex: methods like
void print() const )
-- you cannot modify any member variables
 
K

Karl Heinz Buchegger

Ney André de Mello Zunino wrote:
[snip]
What's the rationale for such restriction? And, lastly, why does
defining a default constructor solve the issue? I.e.:

The code you posted is not your real code.
The code you posted should compile fine without a problem,
since if you don't define any constructor on your own, the compiler
will create a default constructor for you.

So please post your real problem code.
 
M

Max M.

Ney said:
class Test {};

int main()
{
Test t;
const Test u;
}

Why does the definition of 'u' fail?


[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.

Your object is of const POD type, then the last sentence of the paragraph
applies.

MM
 
A

Alf P. Steinbach

* Karl Heinz Buchegger:
Ney André de Mello Zunino wrote:
[snip]
What's the rationale for such restriction? And, lastly, why does
defining a default constructor solve the issue? I.e.:

The code you posted is not your real code.
The code you posted should compile fine without a problem,
since if you don't define any constructor on your own, the compiler
will create a default constructor for you.

So please post your real problem code.

Sorry.

Check out the standard's text quoted by "Max M." in this thread;
some explicit definition of the value is necessary for a const
object.

Unfortunately Visual C++ erronously compiles the code, at least as
I remember it it does.
 
K

Karl Heinz Buchegger

Alf P. Steinbach said:
* Karl Heinz Buchegger:
Ney André de Mello Zunino wrote:
[snip]
What's the rationale for such restriction? And, lastly, why does
defining a default constructor solve the issue? I.e.:

The code you posted is not your real code.
The code you posted should compile fine without a problem,
since if you don't define any constructor on your own, the compiler
will create a default constructor for you.

So please post your real problem code.

Sorry.

Check out the standard's text quoted by "Max M." in this thread;
some explicit definition of the value is necessary for a const
object.

I saw it.
I didn't know that.
I learned.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top