473,413 Members | 2,051 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,413 software developers and data experts.

Member initialization list question

Hi,

Where is the proper place to use a member initialization list, the
header or cpp file?

Does it make any difference?

Also, is there any difference between using a member initialization list
and initializaing member variables in the constructor?

i.e.

MyClass(void) : a(1), b(2) {};

vs

MyClass::MyClass(void)
{
a = 1;
b = 2;
}

Thanks B
Jan 10 '07 #1
5 2642
Bryan <sp**@nospam.comwrote in news:06aph.43401$wc5.41974
@newssvr25.news.prodigy.net:
Hi,

Where is the proper place to use a member initialization list, the
header or cpp file?
Wherever you're defining your constructor.
Does it make any difference?
Yes, probably. But that's only because member bodies which are defined
directly within the definition of a class are implicitly marked as
inlined functions (reminder: inline is only a hint to the compiler, the
compiler is not required to respect the hint).
Also, is there any difference between using a member initialization
list
and initializaing member variables in the constructor?

i.e.

MyClass(void) : a(1), b(2) {};

vs

MyClass::MyClass(void)
{
a = 1;
b = 2;
}
Yes. See the FAQ, section 10.6 (http://www.parashift.com/c++-faq-
lite/ctors.html#faq-10.6)
Jan 10 '07 #2

Bryan wrote:
Hi,

Where is the proper place to use a member initialization list, the
header or cpp file?
The initialisation list is part of the definition of the constructor.
If the constructor definition is in the header, that is where the
initialisation list will be. If the constructor definition is in the
implementation file, that is where the initialisation list will be. Do
you usually put your constructor definitions in the header or the
implementation file? Or does it depend?
Does it make any difference?

Also, is there any difference between using a member initialization list
and initializaing member variables in the constructor?
Using an initialisation list is the only way you can initialise members
in a constructor.
i.e.

MyClass(void) : a(1), b(2) {};

vs

MyClass::MyClass(void)
{
a = 1;
b = 2;
}
The difference is that the latter is NOT initialisation. It is
assignment.
http://www.parashift.com/c++-faq-lit....html#faq-10.6

Gavin Deane

Jan 10 '07 #3
Bryan wrote:
Hi,

Where is the proper place to use a member initialization list, the
header or cpp file?

Does it make any difference?

Also, is there any difference between using a member initialization list
and initializaing member variables in the constructor?

i.e.

MyClass(void) : a(1), b(2) {};
This initializes the variables a and b.
>
vs

MyClass::MyClass(void)
{
a = 1;
b = 2;
}
This assigns values to a and b after they are (unfortunately in this
case) not initialized.

It's entirely analogous to:

int a(1);
and
int a;
a = 1;
Jan 10 '07 #4
Bryan wrote:
Hi,

Where is the proper place to use a member initialization list, the
header or cpp file?
The language syntax requires the init list to go with the definition of
the constructor.
>
Also, is there any difference between using a member initialization list
and initializaing member variables in the constructor?

i.e.

MyClass(void) : a(1), b(2) {};
Delete the semicolon. Delete void -- it's a C-ism.
>
vs

MyClass::MyClass(void)
Delete void -- It's a C-ism.
{
a = 1;
b = 2;
}
Yes. the first case directly constructs a and b. The second case
default constructs a and b, and then assigns them. If members are
expensive to construct and assign, you may see a performance hit (but
only worry about that *AFTER BENCHMARKING*), and the second case will
also fail if either a or b is a const member.
Jan 10 '07 #5
>
Yes. the first case directly constructs a and b. The second case
default constructs a and b, and then assigns them.
Unfortunately, it doesn't default construct a and b. It doesn't
even default initialize them in many cases.
Jan 10 '07 #6

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

Similar topics

15
by: Wolfram Humann | last post by:
Hi, please don't be too harsh if I made stupid errors creating this simple example from my more complex case. Suppose I have a class like this: class BOOK { const string title;
4
by: Avner Flesch | last post by:
Hi, Do you know how can I intialize an array member: for example class A { public: A(int x); };
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 {...
16
by: Steven T. Hatton | last post by:
As far as I know, there is no way to provide a default value for the argument to the constructor A::A(char (&array)) in this example. Correct? struct A{ A(char (&array) ){...
3
by: jut_bit_zx | last post by:
class A { public: A(); virtual ~A(){} .... private: int m_iarray; }
5
by: Randy | last post by:
Hi, When a class contains another class as a member variable, e.g., class ClassA { int a; public :
4
by: Oliver S | last post by:
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? ...
4
by: sks | last post by:
hi , i Have a code snippet as follows class ABC { int &r; ABC(int a=0): r(a) {} }; int main() {
11
by: asdf | last post by:
The oder of member initialization is the order in which the members are defined. So the following code is problematic: class X{ int i; int j; public: X(int val):j(val),i(j){}
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.