Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Consts in Member Initialization list, before or after inherited ctor?

Reply
Thread Tools

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

 
 
Oliver S
Guest
Posts: n/a
 
      05-18-2006
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?


 
Reply With Quote
 
 
 
 
Marcus Kwok
Guest
Posts: n/a
 
      05-18-2006
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
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      05-18-2006
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


 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      05-18-2006
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.
 
Reply With Quote
 
Jonathan Mcdougall
Guest
Posts: n/a
 
      05-18-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
initialization of array as a member using the initialization list aaragon C++ 2 11-02-2008 04:57 PM
about consts and function overloading nick C++ 1 11-13-2005 07:58 AM
static consts Class Vars - Compile Problem in VC6 Baget C++ 5 03-21-2005 11:23 AM
Pointer-to-member-function pointing to a member function of an inherited class akiriwas@gmail.com C++ 12 02-11-2005 05:15 PM
initializing non-static consts trying_to_learn C++ 4 11-07-2004 09:23 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57