anybody knows why?

Discussion in 'C++' started by Yan, Dec 3, 2008.

  1. Yan

    Yan Guest

    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.
    Yan, Dec 3, 2008
    #1
    1. Advertising

  2. On Dec 3, 3:50 pm, 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.


    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
    Maxim Yegorushkin, Dec 3, 2008
    #2
    1. Advertising

  3. 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
    Erik Wikström, Dec 3, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve
    Replies:
    2
    Views:
    554
    Steve
    May 5, 2004
  2. GS
    Replies:
    0
    Views:
    392
  3. Hans Deragon
    Replies:
    2
    Views:
    435
    GrayGeek
    Oct 11, 2003
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,756
    Smokey Grindel
    Dec 2, 2006
  5. Replies:
    4
    Views:
    439
    Randy Howard
    Feb 23, 2005
Loading...

Share This Page