Member initialization list question

Discussion in 'C++' started by Bryan, Jan 10, 2007.

  1. Bryan

    Bryan Guest

    Hi,

    Where is the proper place to use a member initialization list, the
    header or cpp file?

    Does it make any difference?

    Also, is there any difference between using a member initialization list
    and initializaing member variables in the constructor?

    i.e.

    MyClass(void) : a(1), b(2) {};

    vs

    MyClass::MyClass(void)
    {
    a = 1;
    b = 2;
    }

    Thanks B
    Bryan, Jan 10, 2007
    #1
    1. Advertising

  2. Bryan

    Andre Kostur Guest

    Bryan <> wrote in news:06aph.43401$wc5.41974
    @newssvr25.news.prodigy.net:

    > Hi,
    >
    > Where is the proper place to use a member initialization list, the
    > header or cpp file?


    Wherever you're defining your constructor.

    > Does it make any difference?


    Yes, probably. But that's only because member bodies which are defined
    directly within the definition of a class are implicitly marked as
    inlined functions (reminder: inline is only a hint to the compiler, the
    compiler is not required to respect the hint).

    > Also, is there any difference between using a member initialization

    list
    > and initializaing member variables in the constructor?
    >
    > i.e.
    >
    > MyClass(void) : a(1), b(2) {};
    >
    > vs
    >
    > MyClass::MyClass(void)
    > {
    > a = 1;
    > b = 2;
    > }


    Yes. See the FAQ, section 10.6 (http://www.parashift.com/c -faq-
    lite/ctors.html#faq-10.6)
    Andre Kostur, Jan 10, 2007
    #2
    1. Advertising

  3. Bryan

    Gavin Deane Guest

    Bryan wrote:
    > Hi,
    >
    > Where is the proper place to use a member initialization list, the
    > header or cpp file?


    The initialisation list is part of the definition of the constructor.
    If the constructor definition is in the header, that is where the
    initialisation list will be. If the constructor definition is in the
    implementation file, that is where the initialisation list will be. Do
    you usually put your constructor definitions in the header or the
    implementation file? Or does it depend?

    > Does it make any difference?
    >
    > Also, is there any difference between using a member initialization list
    > and initializaing member variables in the constructor?


    Using an initialisation list is the only way you can initialise members
    in a constructor.

    > i.e.
    >
    > MyClass(void) : a(1), b(2) {};
    >
    > vs
    >
    > MyClass::MyClass(void)
    > {
    > a = 1;
    > b = 2;
    > }


    The difference is that the latter is NOT initialisation. It is
    assignment.
    http://www.parashift.com/c -faq-lite/ctors.html#faq-10.6

    Gavin Deane
    Gavin Deane, Jan 10, 2007
    #3
  4. Bryan

    red floyd Guest

    Bryan wrote:
    > Hi,
    >
    > Where is the proper place to use a member initialization list, the
    > header or cpp file?
    >

    The language syntax requires the init list to go with the definition of
    the constructor.

    >
    > Also, is there any difference between using a member initialization list
    > and initializaing member variables in the constructor?
    >
    > i.e.
    >
    > MyClass(void) : a(1), b(2) {};

    Delete the semicolon. Delete void -- it's a C-ism.
    >
    > vs
    >
    > MyClass::MyClass(void)

    Delete void -- It's a C-ism.
    > {
    > a = 1;
    > b = 2;
    > }
    >


    Yes. the first case directly constructs a and b. The second case
    default constructs a and b, and then assigns them. If members are
    expensive to construct and assign, you may see a performance hit (but
    only worry about that *AFTER BENCHMARKING*), and the second case will
    also fail if either a or b is a const member.
    red floyd, Jan 10, 2007
    #4
  5. Bryan

    Ron Natalie Guest

    Bryan wrote:
    > Hi,
    >
    > Where is the proper place to use a member initialization list, the
    > header or cpp file?
    >
    > Does it make any difference?
    >
    > Also, is there any difference between using a member initialization list
    > and initializaing member variables in the constructor?
    >
    > i.e.
    >
    > MyClass(void) : a(1), b(2) {};


    This initializes the variables a and b.
    >
    > vs
    >
    > MyClass::MyClass(void)
    > {
    > a = 1;
    > b = 2;
    > }
    >

    This assigns values to a and b after they are (unfortunately in this
    case) not initialized.

    It's entirely analogous to:

    int a(1);
    and
    int a;
    a = 1;
    Ron Natalie, Jan 10, 2007
    #5
  6. Bryan

    Ron Natalie Guest


    >
    > Yes. the first case directly constructs a and b. The second case
    > default constructs a and b, and then assigns them.


    Unfortunately, it doesn't default construct a and b. It doesn't
    even default initialize them in many cases.
    Ron Natalie, Jan 10, 2007
    #6
    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. Replies:
    3
    Views:
    823
    Peter_Julian
    Oct 10, 2005
  2. Replies:
    6
    Views:
    443
    Ron Natalie
    Dec 11, 2005
  3. toton
    Replies:
    5
    Views:
    912
    Victor Bazarov
    Sep 28, 2006
  4. Angus
    Replies:
    1
    Views:
    2,598
  5. aaragon
    Replies:
    2
    Views:
    599
    James Kanze
    Nov 2, 2008
Loading...

Share This Page