anybody knows why?

Y

Yan

The ISO standard says (in 8.5 [dcl.init] paragraph 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.

Does anybody know what is the rationale behind forcing a programmer to
define a default constructor in this case? Why is it that the object
being of const type makes it a special case?

Thank you.
 
M

Maxim Yegorushkin

The ISO standard says (in 8.5 [dcl.init] paragraph 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.

I switched the order of your questions.
Why is it that the object
being of const type makes it a special case?

As a constant object can not be assigned to, it must be initialised
where it is declared.
Does anybody know what is the rationale behind forcing a programmer to
define a default constructor in this case?

The rationale is to avoid uninitialised constant objects.

Default initialisation of non-PODs does not involve default-
initialisation of POD members, so that they are left uninitialised.
The provided default constructor is supposed to initialise those
members.
 
E

Erik Wikström

The ISO standard says (in 8.5 [dcl.init] paragraph 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.

Does anybody know what is the rationale behind forcing a programmer to
define a default constructor in this case? Why is it that the object
being of const type makes it a special case?

Because if you have a class like this:

class Foo {
int i;
};

and then create a const instance of it:

const Foo foo;

and the language allowed it to use the compiler-generated default
constructor you would end up with Foo::i having a random value and no
way to change it. This is most probably not what you want, you probably
want to set Foo::i to some specific value, and then you need to do so in
the constructor. And if you really don't want to set Foo::i to some
specific value and use whatever value it gets, you can still do that
simply by not initialising Foo::i in the constructor.
 

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

Staff online

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top