943,982 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4331
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jan 19th, 2006
0

Class Constructor Shorthand

Expand Post »
I've been reading through a beginning chapter on classes and I have come upon this below:

class Critter
{
public:
Critter(const string& name = ""): m_Name(name) {}
...
};

I was wondering why they wrote the constructor this way. If anyone can point me to some info, I would be very happy.
Reputation Points: 12
Solved Threads: 6
Junior Poster
Geek-Master is offline Offline
156 posts
since Dec 2004
Jan 19th, 2006
0

Re: Class Constructor Shorthand

Constructors can initialise through assignment operator or initialisation lists. Benefit of using initialisation lists is the improved performance. Your code will run faster if you use initialization lists rather than assignment.

http://www.parashift.com/c++-faq-lit....html#faq-10.6
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005
Jan 22nd, 2006
0

Re: Class Constructor Shorthand

Quote originally posted by sunnypalsingh ...
Constructors can initialise through assignment operator or initialisation lists. Benefit of using initialisation lists is the improved performance. Your code will run faster if you use initialization lists rather than assignment.

http://www.parashift.com/c++-faq-lit....html#faq-10.6
I know what constructors do, that wasn't my question. My question was why did they write it this way.

Is it shorthand? If so, is there a long way of writing it?
Reputation Points: 12
Solved Threads: 6
Junior Poster
Geek-Master is offline Offline
156 posts
since Dec 2004
Jan 22nd, 2006
0

Re: Class Constructor Shorthand

I answered your question correctly
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005
Jan 22nd, 2006
0

Re: Class Constructor Shorthand

I believe you are reading too far into my question. I appreciate your help. Let me define my question more specifically.

One person defined a class constructor this way:
C++ Syntax (Toggle Plain Text)
  1. Critter::Critter(int hunger, int boredom):
  2. m_Hunger(hunger),
  3. m_Boredom(boredom)
  4. {}

Another person defined a class constructor this way:
C++ Syntax (Toggle Plain Text)
  1. Box::Box(int ht, int wd, int dp)
  2. {
  3. height = ht;
  4. width = wd;
  5. depth = dp;
  6. }

Besides the fact the two constructors are from different classes. What is the difference of writing it the first way apposed of writing it the second way?

I noticed that the first initializes the members after a colon and the second initializes the members inside the curley braces. So what is the difference?
Reputation Points: 12
Solved Threads: 6
Junior Poster
Geek-Master is offline Offline
156 posts
since Dec 2004
Jan 22nd, 2006
0

Re: Class Constructor Shorthand

Oh man you didn't read the link....
You can do both ways...first one through initilisation lists and second one is through assignment operator.
First one is better...for more information read my first post.
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005
Jan 22nd, 2006
0

Re: Class Constructor Shorthand

Oh so my first post was using an initializing list. I was under the impression that it was the same as an assignment constructor, just written in "shorthand" I thought. So when you use an assignment constructor it passes a temporary object that isn't efficient. Yet an initializing list does it all in one instance. Cool

One last question about the initializing list.

C++ Syntax (Toggle Plain Text)
  1. Fred::Fred() : x_(whatever) {}

When you create your Fred object, will anything that is passed to whatever be assigned to x_ using the initializing list?
Reputation Points: 12
Solved Threads: 6
Junior Poster
Geek-Master is offline Offline
156 posts
since Dec 2004
Jan 22nd, 2006
0

Re: Class Constructor Shorthand

>will anything that is passed to whatever be assigned to x_ using the initializing list?
Assuming whatever is a compatible initializer for x_, yes.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jan 22nd, 2006
0

Re: Class Constructor Shorthand

Another small thing to add that I read in the book C++ in Action .
Say you have a const member declared inside the C++ class.
C++ Syntax (Toggle Plain Text)
  1. class Planet
  2. {
  3. public:
  4. Planet( int iWeight);
  5. private:
  6. const int weight;
  7. }

since the weight member cant be assigned a value in a standard contructor like this

C++ Syntax (Toggle Plain Text)
  1. Planet::Planet( int iWeight)
  2. {
  3. weight = iWeight ; // wrong
  4. }
The only way you can do that is using an initialization list.

C++ Syntax (Toggle Plain Text)
  1. Planet::Planet( int iWeight):weight(iWeight) // correct
  2. {
  3. }
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Jan 22nd, 2006
0

Re: Class Constructor Shorthand

Quote originally posted by WolfPack ...
Another small thing to add that I read in the book C++ in Action .
Say you have a const member declared inside the C++ class.
C++ Syntax (Toggle Plain Text)
  1. class Planet
  2. {
  3. public:
  4. Planet( int iWeight);
  5. private:
  6. const int weight;
  7. }

since the weight member cant be assigned a value in a standard contructor like this

C++ Syntax (Toggle Plain Text)
  1. Planet::Planet( int iWeight)
  2. {
  3. weight = iWeight ; // wrong
  4. }
The only way you can do that is using an initialization list.

C++ Syntax (Toggle Plain Text)
  1. Planet::Planet( int iWeight):weight(iWeight) // correct
  2. {
  3. }
Since weight is of Private Access you can't directly initialize it.

So an Initialization list has the ability to indirectly access the data member weight and maintain encapsulation, right?
Reputation Points: 12
Solved Threads: 6
Junior Poster
Geek-Master is offline Offline
156 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Segmentation Fault
Next Thread in C++ Forum Timeline: C++ second largest problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC