954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Class Constructor Shorthand

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.

Geek-Master
Junior Poster
158 posts since Dec 2004
Reputation Points: 12
Solved Threads: 7
 

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-lite/ctors.html#faq-10.6

SpS
Posting Pro
599 posts since Aug 2005
Reputation Points: 70
Solved Threads: 32
 

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-lite/ctors.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?

Geek-Master
Junior Poster
158 posts since Dec 2004
Reputation Points: 12
Solved Threads: 7
 

I answered your question correctly

SpS
Posting Pro
599 posts since Aug 2005
Reputation Points: 70
Solved Threads: 32
 

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:

Critter::Critter(int hunger, int boredom):
     m_Hunger(hunger),
     m_Boredom(boredom)
{}


Another person defined a class constructor this way:

Box::Box(int ht, int wd, int dp)
{
    height = ht;
    width = wd;
    depth = dp;
}


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?

Geek-Master
Junior Poster
158 posts since Dec 2004
Reputation Points: 12
Solved Threads: 7
 

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
Posting Pro
599 posts since Aug 2005
Reputation Points: 70
Solved Threads: 32
 

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.

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?

Geek-Master
Junior Poster
158 posts since Dec 2004
Reputation Points: 12
Solved Threads: 7
 

>will anything that is passed to whatever be assigned to x_ using the initializing list?
Assuming whatever is a compatible initializer for x_, yes.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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.

class Planet
{
public:
         Planet( int iWeight);
private:
         const int weight;
}


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

Planet::Planet( int iWeight)
{
       weight = iWeight ; // wrong
}

The only way you can do that is using an initialization list.

Planet::Planet( int iWeight):weight(iWeight) // correct
{
}
WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 

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.

class Planet
{
public:
         Planet( int iWeight);
private:
         const int weight;
}

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

Planet::Planet( int iWeight)
{
       weight = iWeight ; // wrong
}

The only way you can do that is using an initialization list.

Planet::Planet( int iWeight):weight(iWeight) // correct
{
}

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?

Geek-Master
Junior Poster
158 posts since Dec 2004
Reputation Points: 12
Solved Threads: 7
 

The accessibility of the data member does not matter here. Even if you declare weight public you will not be able to use the standard constructor.

What we use the initialization list here is for initializing const data members in a class.

We have declared weight to be const. So when you define an object of type Planet, you must give a weight for it.

e.g.

Planet earth( 2000 );


In the standard constructor you use the

weight = iWeight


statement which is illegal.

The only way is to use the initialization list.

Look for thisThe keyword const means that nobody can change the value of _identifier during the lifetime of the object. It is physically (or at least "compilatorily") impossible. But even a const has to be initialized some place or another. The compiler provides just one little window when we can (and in fact have to) initialize a const data member. It is in the preamble to the constructor. Not even in the body of the constructor--only in the preamble itself can we initialize a const data member. Preamble is the part of the constructor that starts with a colon and contains a comma-separated list of data members followed by their initializers in parentheses. In our preamble _identifier is initialized with the value of id. in this page of the book I have linked. you will get a more clear idea of it's usage.

WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You