Class Constructor Shorthand

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2004
Posts: 149
Reputation: Geek-Master is an unknown quantity at this point 
Solved Threads: 6
Geek-Master's Avatar
Geek-Master Geek-Master is offline Offline
Junior Poster

Class Constructor Shorthand

 
0
  #1
Jan 19th, 2006
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.
If in doubt, reach into the trash can and remove the user guide.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: Class Constructor Shorthand

 
0
  #2
Jan 19th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 149
Reputation: Geek-Master is an unknown quantity at this point 
Solved Threads: 6
Geek-Master's Avatar
Geek-Master Geek-Master is offline Offline
Junior Poster

Re: Class Constructor Shorthand

 
0
  #3
Jan 22nd, 2006
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?
If in doubt, reach into the trash can and remove the user guide.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: Class Constructor Shorthand

 
0
  #4
Jan 22nd, 2006
I answered your question correctly
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 149
Reputation: Geek-Master is an unknown quantity at this point 
Solved Threads: 6
Geek-Master's Avatar
Geek-Master Geek-Master is offline Offline
Junior Poster

Re: Class Constructor Shorthand

 
0
  #5
Jan 22nd, 2006
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:
  1. Critter::Critter(int hunger, int boredom):
  2. m_Hunger(hunger),
  3. m_Boredom(boredom)
  4. {}

Another person defined a class constructor this way:
  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?
If in doubt, reach into the trash can and remove the user guide.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: Class Constructor Shorthand

 
0
  #6
Jan 22nd, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 149
Reputation: Geek-Master is an unknown quantity at this point 
Solved Threads: 6
Geek-Master's Avatar
Geek-Master Geek-Master is offline Offline
Junior Poster

Re: Class Constructor Shorthand

 
0
  #7
Jan 22nd, 2006
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.

  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?
If in doubt, reach into the trash can and remove the user guide.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,740
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 739
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Class Constructor Shorthand

 
0
  #8
Jan 22nd, 2006
>will anything that is passed to whatever be assigned to x_ using the initializing list?
Assuming whatever is a compatible initializer for x_, yes.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Class Constructor Shorthand

 
0
  #9
Jan 22nd, 2006
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.
  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

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

  1. Planet::Planet( int iWeight):weight(iWeight) // correct
  2. {
  3. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 149
Reputation: Geek-Master is an unknown quantity at this point 
Solved Threads: 6
Geek-Master's Avatar
Geek-Master Geek-Master is offline Offline
Junior Poster

Re: Class Constructor Shorthand

 
0
  #10
Jan 22nd, 2006
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.
  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

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

  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?
If in doubt, reach into the trash can and remove the user guide.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC