Member Avatar for Geek-Master

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.

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

Member Avatar for Geek-Master

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?

I answered your question correctly

Member Avatar for Geek-Master

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?

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.

Member Avatar for Geek-Master

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?

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

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
{
}
Member Avatar for Geek-Master

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?

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 this

The 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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.