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

O

Oliver S

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?
 
M

Marcus Kwok

Oliver S said:
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.
 
V

Victor Bazarov

Oliver said:
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
 
P

Pete Becker

Oliver said:
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.
 
J

Jonathan Mcdougall

Marcus said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top