Consts in Member Initialization list, before or after inherited ctor?

Discussion in 'C++' started by Oliver S, May 18, 2006.

  1. Oliver S

    Oliver S Guest

    I got a class, a VCL form actually. I have two const member that I am
    initializing (in the order they were declared). My question is, do I put
    them before calling inherited constructor or after? In other works, should
    it look like this or like that?

    __fastcall TfMJTuner::TfMJTuner(TComponent* Owner)
    : OriginalWidth(Width), OriginalHeight(Height), TForm(Owner)

    or

    __fastcall TfMJTuner::TfMJTuner(TComponent* Owner)
    : TForm(Owner), OriginalWidth(Width), OriginalHeight(Height)


    The damn thing works either way, I wonder which way is the proper way?
     
    Oliver S, May 18, 2006
    #1
    1. Advertising

  2. Oliver S

    Marcus Kwok Guest

    Oliver S <> wrote:
    > I got a class, a VCL form actually. I have two const member that I am
    > initializing (in the order they were declared). My question is, do I put
    > them before calling inherited constructor or after? In other works, should
    > it look like this or like that?
    >
    > __fastcall TfMJTuner::TfMJTuner(TComponent* Owner)
    > : OriginalWidth(Width), OriginalHeight(Height), TForm(Owner)
    >
    > or
    >
    > __fastcall TfMJTuner::TfMJTuner(TComponent* Owner)
    > : TForm(Owner), OriginalWidth(Width), OriginalHeight(Height)
    >
    >
    > The damn thing works either way, I wonder which way is the proper way?


    My reasoning would be to put the base class constructor first, since
    logically you want to fully construct the base class object before
    initializing the derived part of the class.

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
     
    Marcus Kwok, May 18, 2006
    #2
    1. Advertising

  3. Oliver S wrote:
    > I got a class, a VCL form actually. I have two const member that I am
    > initializing (in the order they were declared). My question is, do I
    > put them before calling inherited constructor or after? In other
    > works, should it look like this or like that?
    >
    > __fastcall TfMJTuner::TfMJTuner(TComponent* Owner)
    > : OriginalWidth(Width), OriginalHeight(Height), TForm(Owner)
    >
    > or
    >
    > __fastcall TfMJTuner::TfMJTuner(TComponent* Owner)
    > : TForm(Owner), OriginalWidth(Width), OriginalHeight(Height)
    >
    >
    > The damn thing works either way, I wonder which way is the proper way?


    The "proper" way would be *after* the inherited (base) classes. The reason
    is not to define the order in which they are initialised (the C++ Standard
    mandates the order and it's "base classes first, in the order of
    declaration,
    then members, in the order of declaration"), but to reflect the predefined
    order and not confuse yourself or anybody who reads the code later.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, May 18, 2006
    #3
  4. Oliver S

    Pete Becker Guest

    Re: Consts in Member Initialization list, before or after inheritedctor?

    Oliver S wrote:
    > I got a class, a VCL form actually. I have two const member that I am
    > initializing (in the order they were declared). My question is, do I put
    > them before calling inherited constructor or after?
    >


    It doesn't matter. The order is determined by the order of their
    declarations within the class. Look it up.

    --

    Pete Becker
    Roundhouse Consulting, Ltd.
     
    Pete Becker, May 18, 2006
    #4
  5. Marcus Kwok wrote:
    > Oliver S <> wrote:
    > > I got a class, a VCL form actually. I have two const member that I am
    > > initializing (in the order they were declared). My question is, do I put
    > > them before calling inherited constructor or after? In other works, should
    > > it look like this or like that?
    > >
    > > __fastcall TfMJTuner::TfMJTuner(TComponent* Owner)
    > > : OriginalWidth(Width), OriginalHeight(Height), TForm(Owner)
    > >
    > > or
    > >
    > > __fastcall TfMJTuner::TfMJTuner(TComponent* Owner)
    > > : TForm(Owner), OriginalWidth(Width), OriginalHeight(Height)
    > >
    > >
    > > The damn thing works either way, I wonder which way is the proper way?

    >
    > My reasoning would be to put the base class constructor first, since
    > logically you want to fully construct the base class object before
    > initializing the derived part of the class.


    This is done automatically by the compiler. The order in the
    initialization list does not matter (except for a "logical" order to
    help you and your coworkers). In fact, this:

    class A
    {
    public:
    A()
    : b(5), a(b)
    {
    }

    private:
    int a, b; // <-- note the order!
    };

    is trouble. Since 'b' was declared after 'a', it will be initialized
    after. That means 'a' will use 'b' before it is initialized (undefined
    behavior). See http://gotw.ca/gotw/080.htm for more informations on the
    order of initialization in a class hierarchy.


    Jonathan
     
    Jonathan Mcdougall, May 18, 2006
    #5
    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. Apricot
    Replies:
    4
    Views:
    560
    velthuijsen
    Apr 16, 2004
  2. Matthias Kaeppler
    Replies:
    2
    Views:
    469
    Victor Bazarov
    Jul 18, 2005
  3. NVH
    Replies:
    8
    Views:
    520
    mlimber
    Jul 6, 2006
  4. Grizlyk
    Replies:
    8
    Views:
    514
    Grizlyk
    Nov 29, 2006
  5. , India

    copy ctor vs default ctor

    , India, Aug 15, 2007, in forum: C++
    Replies:
    2
    Views:
    432
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Aug 15, 2007
Loading...

Share This Page