[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

anybody knows why?

Yan

12/3/2008 3:50:00 PM

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.
2 Answers

Maxim Yegorushkin

12/3/2008 4:41:00 PM

0

On Dec 3, 3:50 pm, Yan <yvinogra...@gmail.com> wrote:
> 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.

--
Max

Erik Wikström

12/3/2008 4:46:00 PM

0

On 2008-12-03 16:50, Yan wrote:
> 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.

--
Erik Wikström