473,587 Members | 2,487 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::TfMJ Tuner(TComponen t* Owner)
: OriginalWidth(W idth), OriginalHeight( Height), TForm(Owner)

or

__fastcall TfMJTuner::TfMJ Tuner(TComponen t* Owner)
: TForm(Owner), OriginalWidth(W idth), OriginalHeight( Height)
The damn thing works either way, I wonder which way is the proper way?
May 18 '06 #1
4 1538
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::TfMJ Tuner(TComponen t* Owner)
: OriginalWidth(W idth), OriginalHeight( Height), TForm(Owner)

or

__fastcall TfMJTuner::TfMJ Tuner(TComponen t* Owner)
: TForm(Owner), OriginalWidth(W idth), 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::TfMJ Tuner(TComponen t* Owner)
: OriginalWidth(W idth), OriginalHeight( Height), TForm(Owner)

or

__fastcall TfMJTuner::TfMJ Tuner(TComponen t* Owner)
: TForm(Owner), OriginalWidth(W idth), 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::TfMJ Tuner(TComponen t* Owner)
: OriginalWidth(W idth), OriginalHeight( Height), TForm(Owner)

or

__fastcall TfMJTuner::TfMJ Tuner(TComponen t* Owner)
: TForm(Owner), OriginalWidth(W idth), 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
9450
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, int b=1, int c=1) : _a(a), _b(b), _c(c) {}
10
3512
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 { lengthSV=16 }; // Length of SomeVector vector<double> SomeVector;
3
5928
by: jut_bit_zx | last post by:
class A { public: A(); virtual ~A(){} .... private: int m_iarray; }
3
6590
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 &qr, Q3Canvas *canvas); Mosfet::~Mosfet();
3
1538
by: josh | last post by:
Hi, if I've: class Date { public: Date(int m, int d, int y); }
3
19263
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? Is it possible for a class to have const variables? As just declaring the class alone doesn't create any memory, only creation of objects results...
16
2992
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
1823
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; class Base { public: Base(int x = 0);
2
3486
by: aaragon | last post by:
Hello everyone, Is this valid? template <class A> struct ClassA { typedef A value_type; value_type* data_;
0
7920
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7849
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8215
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8347
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7973
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6626
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3844
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1189
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.