473,385 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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?
May 18 '06 #1
4 1531
Oliver S <no****@nosite.org> 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
May 18 '06 #2
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
May 18 '06 #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?


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.
May 18 '06 #4
Marcus Kwok wrote:
Oliver S <no****@nosite.org> 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

May 18 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Michael McKnerney | last post by:
Hi, It seems I can influence how a base class is initialized beyond the 'normal' manner and I was wondering if someone can tell me why this. Here's my example. class A { public: A(int a=1,...
10
by: Fred Ma | last post by:
Are there any reasons that would make it bad for C++ to allow simultaneous declaration and initilization of member data? Current way: ------------ class DerivedClass : BaseClass { { enum {...
3
by: jut_bit_zx | last post by:
class A { public: A(); virtual ~A(){} .... private: int m_iarray; }
3
by: Jeff | last post by:
Its been years since I did C++ and Im unsure what this is about. I have a QT class Im inheriting from in my .h file: class Mosfet : Q3CanvasRectangle { public: Mosfet::Mosfet(const QRect...
3
by: josh | last post by:
Hi, if I've: class Date { public: Date(int m, int d, int y); }
3
by: sam_cit | last post by:
Hi Everyone, When a variable is declared as const, it is initialized along with the declaration, as modification later on is not possible. Having seen that, how about const variables in a class?...
16
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; class Test { static Test t; static Test init_Test( ) { return t; }
2
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; class Base { public: Base(int x = 0);
2
by: aaragon | last post by:
Hello everyone, Is this valid? template <class A> struct ClassA { typedef A value_type; value_type* data_;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.