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

Two questions about...something


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) { }
}

I can see what it does, but...why? Why is it written
this way, and not like:

class C {
private:
int value;
public:
C(int initValue) {
value = initValue;
}
}

The first snippet looks like some inherit-stuff, but
obviously it's initializing the private member. So why
is this syntax used, and is it at all different from
the second snippet quality-wise?

--
Fred H

void FredH::Contact() {
TextToSpeach.say("frode at age dee dee dot en oh");
}
Jul 22 '05 #1
14 1469
"Fred H" <se****@nospam.com> wrote...

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) { }
}

I can see what it does, but...why? Why is it written
this way, and not like:

class C {
private:
int value;
public:
C(int initValue) {
value = initValue;
}
}

The first snippet looks like some inherit-stuff, but
obviously it's initializing the private member. So why
is this syntax used, and is it at all different from
the second snippet quality-wise?


This is covered in the FAQ. Please read FAQ before posting.

I think you started reading the STL book too early. You need
a C++ book and enough time with it so you don't ask questions
like that.

Victor
Jul 22 '05 #2
Fred H wrote:

The first snippet looks like some inherit-stuff, but
obviously it's initializing the private member. So why
is this syntax used, and is it at all different from
the second snippet quality-wise?


1)
The first snipped is initialization, the second one
assignment. Here with the int it doesnt make much
difference. But consider:

class C {
private:
SomeExpensiveClass value;
public:
C(SomeExpensiveClass initValue) : value(initValue) { }
}

In the first snipped the initValue is used to construct the
value.
In the second snipped value is default constructed. Then
initValue is assigned to value. This is most often very
inefficient.

2)
class C {
private:
const int value;
public:
C(int initValue) : value(initValue) { }
}

Const members can only be initialized, not set. So the
second snippet doesnt work with the above code.

3)
Using initialization instead of assignment is considered
good style. So prefer to write your constructors like the
first snipped.

hth

Christoph
Jul 22 '05 #3

"Fred H" <se****@nospam.com> wrote in message
news:op**************@news.mimer.no...

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) { }
}

I can see what it does, but...why? Why is it written
this way, and not like:

class C {
private:
int value;
public:
C(int initValue) {
value = initValue;
}
}

The first snippet looks like some inherit-stuff, but
obviously it's initializing the private member. So why
is this syntax used, and is it at all different from
the second snippet quality-wise?


The first is initialization, the second is assignment. Assignment and
initialization are not the same thing.

In the second example value is first default initalized and then assigned.
For an integer this make no difference because default initialization is a
no-op. But for a class it might not be.

class X()
{
X(); // default initialization, expensive
X(const X&) // copy initialization, expensive
X& operator=(const X&); // assignment, expensive
};

Now this code only does copy initialization

class C
{
C(const X& xx) : x(xx) {}
X x;
};

But this code does default initialization followed by assignment

class C
{
C(const X& xx) { x = xx; }
X x;
};

That's two expensive operations compared to one.

So it's generally thought to be good style to prefer initialzation to
assignment in constructors, even when it makes no actual difference, as is
the case with int's.

john
Jul 22 '05 #4
Fred H wrote:
class C {
private:
int value;
public:
C(int initValue) : value(initValue) { }
} class C {
private:
int value;
public:
C(int initValue) {
value = initValue;
}
}

The first snippet looks like some inherit-stuff, but
obviously it's initializing the private member. So why
is this syntax used, and is it at all different from
the second snippet quality-wise?


Read /Effective C++/ by Jon Bon Jovi.

Stare at this expression:

C(int initValue) /*yo*/ {
value = initValue;
}

In the place where I added /*yo*/ there's a trivial constructor for 'value'.
Because 'value' is an 'int', that constructor is so trivial it does nothing.
But it's still conceptually there.

If 'value' were a 'SimCity', not an 'int', then its constructor might not be
trivial. In that case, we should not waste time assigning to the 'SimCity'
again inside the C constructor body.

If there were more than one member, then their constructors, even if they do
nothing, would still be there, and would still be in the same order as they
declare in the class body. And C's destructor (which is also really there)
would destroy them in reverse order. So declaring non-trivial constructions
for each members, in the same order as they declare, improves your cognitive
awareness.

Ultimately, the only reason the STL book did it was to set a good example.
There are some engineer decisions with different costs and benefits, and we
must carefully experiment to make that decision cheaply. In this case, there
are myriad subtle benefits to constructing members with sub-constructor
notation, and no benefits to neglecting their construction. So follow the
STL book's example.
--
Phlip
http://www.xpsd.org/cgi-bin/wiki?Tes...UserInterfaces
Jul 22 '05 #5

Guess who forgot to change the subject line... :\

--
Fred H

void FredH::Contact() {
TextToSpeach.say("frode at age dee dee dot en oh");
}
Jul 22 '05 #6
On Mon, 02 Feb 2004 14:58:08 GMT in comp.lang.c++, Fred H
<se****@nospam.com> was alleged to have written:
C(int initValue) : value(initValue) { }
}

I can see what it does, but...why? Why is it written
this way,


It's called the initialization list, and it means the member variable
itself is constructed according to the specification there, instead of
being initialized according to the default and then assigned some value
in the body of your constructor. Might be mote efficient but probably
makes no difference for an 'int'. Makes a bigger difference if the
member variable is of a class with its own constructor.

This issue is covered in Marshall Cline's C++ FAQ. See the topic
"[10.6] Should my constructors use "initialization lists" or
"assignment"?" It is always good to check the FAQ before posting.
You can get the FAQ at:
http://www.parashift.com/c++-faq-lite/
Jul 22 '05 #7
On Mon, 02 Feb 2004 14:58:08 +0000, Fred H wrote:

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) { }
}

I can see what it does, but...why? Why is it written
this way, and not like:

class C {
private:
int value;
public:
C(int initValue) {
value = initValue;
}
}

The first snippet looks like some inherit-stuff, but
obviously it's initializing the private member. So why
is this syntax used, and is it at all different from
the second snippet quality-wise?


Yes, the second snippet assigns, instead of initialising. For an int, the
net result is exactly the same (given any decent compiler) but if value is
some complex object there can be a huge difference.

Apart from that, some things can only be initialised, not assigned to.
Const objects are one, references another, and maybe I missed one.

HTH,
M4

Jul 22 '05 #8

"Fred H" <se****@nospam.com> wrote in message
news:op**************@news.mimer.no...

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) { }
}

I can see what it does, but...why? Why is it written
this way, and not like:

class C {
private:
int value;
public:
C(int initValue) {
value = initValue;
}
}

The first snippet looks like some inherit-stuff, but
obviously it's initializing the private member. So why
is this syntax used, and is it at all different from
the second snippet quality-wise?


No, in this case it's basically the same. There are 2 reasons (IMO) this is
used.
1) to be consistent with initializing *all* parts of the class. Your
private member is an int. What if it were an instance of another class?
Then you'd have to call the constructor for that class to initialize it
(assuming it had a constructor with some parameters.) Think of your in
variable as an object.
2) in general it's always better to initialize variables ASAP. This is true
in traditional programming too. Always initialiaze your variables as soon
as you can. With C++ and OO, you have a new method for initializing
variables even sooner - before your constructor code actually starts.
Exploit this.
Jul 22 '05 #9

[Completely OT]
This is covered in the FAQ. Please read FAQ before posting.

I think you started reading the STL book too early. You need
a C++ book and enough time with it so you don't ask questions
like that.

Victor


I have read the FAQ, and I search it quite often. But this time
I didn't know what to search for. Now I know that I could have
searched for "initializaion", and found FAQ 10.6, but hey, I can't
read the whole FAQ through every time I have a question, can I...?

Regarding starting the STL book too early, you might be right. I was
hoping it would suffice to read half a dozen or so online tutorials,
and then start using my STL book, but I'm comming to the conclusion
that I might actually need to buy a good tutorial/reference book. So
I've started reaserching for good books.

But I still can't see why people like you allways need to tell people
like me, that we really ought not to post in this group.

What kind of activities is it you'd like there to be in this group,
if newbies just isn't allowed here? I allways thought that
comp.lang.c++.moderated was the newbie free zone around here...

[/Completely OT]

To all you other good people who answered my question: Thanks a lot :-)
--
Fred H

void FredH::Contact() {
TextToSpeach.say("frode at age dee dee dot en oh");
}
Jul 22 '05 #10
Fred H wrote:

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) { }
}

I can see what it does, but...why? Why is it written
this way, and not like:

class C {
private:
int value;
public:
C(int initValue) {
value = initValue;
}
}

The first snippet looks like some inherit-stuff, but
obviously it's initializing the private member. So why
is this syntax used, and is it at all different from
the second snippet quality-wise?


The first one is a true initialization, while the second
one is an assignment. Granted it doesn't make much of
a difference in this specific case. But there are situations
where it makes a difference.

Consider a class which holds a constant. To give this
constant a value you can onyl use initialization, not
assignement.

So:

....
private:
const int value;
....

public:
C( int Init ) : value( Init ) {}
....

works (because the above is initialization and a constant can be
initialized), while

C( int Init ) { value = Init; }

will not work, because you cannot assign anything to a constant.

There are other cases too, where it is important to use an initializer
list. Consult your text book about those.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #11
On Mon, 02 Feb 2004 15:47:16 GMT in comp.lang.c++, Fred H
<se****@nospam.com> was alleged to have written:
I have read the FAQ, and I search it quite often. But this time
I didn't know what to search for. Now I know that I could have
searched for "initializaion",


Even though I flog the FAQ too, I agree with you completely in this
case. It is hidden like an Easter egg if you do not know the name.
All you can do is memorize the whole FAQ... learn Stroustrup while you
are at it.

Please do not take FAQ pointers as meaning that you should not post.
Rather, we hope that having the FAQ might reduce the need to do so in
every case.

Jul 22 '05 #12
"Fred H" <se****@nospam.com> wrote...
[...]
But I still can't see why people like you allways need to tell people
like me, that we really ought not to post in this group.
I never tell anybody not to post. It's simply not my place to tell
people what to do. However, when you post, you ask for advice and
invite comment. If you're not prepared to read any reasonable comment
to your post, you should refrain from posting, that's all.
What kind of activities is it you'd like there to be in this group,
if newbies just isn't allowed here? I allways thought that
comp.lang.c++.moderated was the newbie free zone around here...


Around here? comp.lang.c++.moderated is a different newsgroup. It
has its own rules, written or not, its own frequents, and I assure
you, it just as much welcomes newbies as this one (no less, no more).

If you do feel you need to attend a kindergarten first (judging by
your inability to exist in the real world without crying at the first
sign of flame or criticism), visit alt.comp.lang.learn.c-c++

Good luck!

And don't forget to read the entire FAQ before EVER posting here again.
Jul 22 '05 #13
Fred H wrote:

[Completely OT]
This is covered in the FAQ. Please read FAQ before posting.

I think you started reading the STL book too early. You need
a C++ book and enough time with it so you don't ask questions
like that.

Victor

I have read the FAQ, and I search it quite often. But this time
I didn't know what to search for.


It's cool then. Yep - sometimes you just don't know the terminology and
you don't know what you're looking for.

Jul 22 '05 #14
If you do feel you need to attend a kindergarten first (judging by
your inability to exist in the real world without crying at the first
sign of flame or criticism), visit alt.comp.lang.learn.c-c++
I'm sure we can coexist...in the real world. No need for kindergarten
for any of us I hope ;)

And don't forget to read the entire FAQ before EVER posting here again.


:D

--
Fred H

void FredH::Contact() {
TextToSpeach.say("frode at age dee dee dot en oh");
}
Jul 22 '05 #15

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

Similar topics

4
by: Trevor Best | last post by:
Does anyone know of a source of some good exam (pop quiz) type questions on Access? Looking to test candidates in a job interview. Pref a mix of multiple choice and ones that have real answers. ...
7
by: Rano | last post by:
/* Hello, I've got some troubles with a stupid program... In fact, I just start with the C language and sometime I don't understand how I really have to use malloc. I've readden the FAQ...
5
by: John Sheppard | last post by:
Hi all, I am not sure that I am posting this in the right group but here it goes anyway. I am new to socket programming and I have been searching on the internet to the questions I am about to pose...
13
by: M.Siler | last post by:
Let me clarify from my last post. I am not using these 4 questions as the sole screening method. Currently in, the Tampa Bay area (Florida) there is an extreme shortage of C# developers. We have...
30
by: James Conrad StJohn Foreman | last post by:
After 3 years of using DB2 on Linux, I'm leaving my current employers to go work for a SQL Server shop instead. In order to find my replacement, they're trying to put together a set of questions...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
102
by: dreamznatcher | last post by:
Hello, I'm considering a career switch to a more database-related job, but need help on a few questions and issues. I'm a Computer Engineering graduate and have always felt most comfortable...
22
by: gustum | last post by:
Im graduating in the coming december. Anyone pls guide me where i can get c++ interview questions. I shall be very thankful to you if you provide me good link so any stuff if you have regarding...
16
by: =?Utf-8?B?VGFtbXkgTmVqYWRpYW4=?= | last post by:
Hi, I am using Visual C# window to dispaly a set of questions with their answers. The users should be able to move to the next question by clicking on next button. I am going to use only one panel...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...

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.