473,408 Members | 1,904 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,408 software developers and data experts.

Quick question about initializing

Given a simple class like

class test {
private:
size_t size_;
int* data_;
public:
test(size_t s)
: size_(s), data_(new int[s]
{ /* ... */ };
};

I'm curious as to whether there's any differance if I change the
initialization list to:

: size(s), data_(new int[size_])

if we disregard the possible trouble that might cause if I happened to
declare data_ first?

--
Erik Wikström

Nov 15 '06 #1
2 1885


It's also important to know the order in which the initializers will be
evaluated. For example, the following definition will cause problems:
class Thingy : public BaseThingy {
public:
Thingy(int endpoint_, int count_) : last(endpoint_), BaseThingy(),
first(last - count_) { }
private:
int first;
int last;
};
The reason this is a problem is that initializers in the constructor
are not executed in the order they are listed, but rather the base
class is always initialized first, followed by the other member
variables in the order those variables are declared in the class
definition. (Then the body of the constructor will be executed after
all the members are initialized.) Thus the three initializers in the
constructor for Thingy will be executed in this order:
BaseThingy() // The base class constructor
first(last - count_)
last(endpoint_)
The problem occurs when initializing the member first, as the
expression calculating the value for first contains a reference to
last-but last has not been initialized yet!

The recommended practice is to always list the initializers in the
order they will be executed.

....

Afterwards it was mentioned also this:

However, I disagree that having a long list of initialized attributes
is necessarily bad. For example, const and reference members must have
initializers. Also, for many C++ experts (see for example Scott Meyers'
Effective C++, Item #12), using initializers for member variables is
the preferred practice for cases where an initializer is possible and
the expressions necessary for producing the constructor arguments don't
make the code unsafe or unreadable. The main reason for this is
efficiency.

I found the texts from:
http://www.physicsforums.com/archive...p/t-67914.html

If you would have data_ before size_ in the attribute list like this:

class test {
private:
int* data_; // <---
size_t size_;

public:
test(size_t s)
: size_(s), data_(new int[s]
{ /* ... */ };
};
then size_ would not have been initialized, when data_ is initialized:
: size_(s), data_(new int[size_])
-You would initialize data_ with an uninitialize size_.

size_ would be initialized after data_ is initialized (with an
uninitialized size_).
But your current code might work ok.
I hope that is the way it goes.

Nov 15 '06 #2

<er****@student.chalmers.sewrote in message
news:11********************@b28g2000cwb.googlegrou ps.com...
Given a simple class like

class test {
private:
size_t size_;
int* data_;
public:
test(size_t s)
: size_(s), data_(new int[s]
{ /* ... */ };
};

I'm curious as to whether there's any differance if I change the
initialization list to:

: size(s), data_(new int[size_])

if we disregard the possible trouble that might cause if I happened to
declare data_ first?
Also disregarding the errors? :-) (You left off the closing parentheses in
the first example, and the ending underscore for size_ in the second.)

I guess you're just asking if using s versus size_ makes a difference. In
this case, probably not. (There might be a small performance difference on
some compilers, especially in unoptimized (i.e., debug) code, but I doubt
it. You could check the assembler output from each to see what yours does.)

But as long as size_ is declared before data_, it will get initialized
first, so there should be no problems. (I prefer the first version, though,
just in case the declaration order DOES get changed!)

-Howard


Nov 15 '06 #3

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

Similar topics

14
by: Fred H | last post by:
I'm reading a STL book right now, and there I've picked up the folowing syntax: class C { private: int value; public: C(int initValue) : value(initValue) { } }
24
by: David Mathog | last post by:
If this: int i,sum; int *array; for(sum=0, i=0; i<len; i++){ sum += array; } is converted to this (never mind why for the moment):
47
by: sunglo | last post by:
Some time a go, in a discussion here in comp.lang.c, I learnt that it's better not to use a (sometype **) where a (void **) is expected (using a cast). Part of the discussion boiled down to the...
3
by: Jess | last post by:
Hello, I've been reading Effective C++ about multiple inheritance, but I still have a few questions. Can someone give me some help please? First, it is said that if virtual inheritance is...
13
by: Aarti | last post by:
I have a very elementary question about pointers. Please pardon me for my ignorance of C int main() { int* i; *i = 1 //at times this may give me a core dump. const char* str = "test"; //This...
5
by: r035198x | last post by:
Setting up. Getting started To get started with java, one must download and install a version of Sun's JDK (Java Development Kit). The newest release at the time of writting this article is...
0
by: r035198x | last post by:
Inheritance We have already covered one important concept of object-oriented programming, namely encapsulation, in the previous article. These articles are not articles on object oriented...
13
by: WaterWalk | last post by:
Hello. When I consult the ISO C++ standard, I notice that in paragraph 3.6.2.1, the standard states: "Objects with static storage duration shall be zero-initialized before any other...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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
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.