(arg): Is this shorthand?

Hi all,

I've just start with Beginning C++ Through Game Programming. I'm stuck on Chapter 8, with a section of syntax I don't understand. The line in question is

1
2
3
4
5
Critter::Critter(int hunger):
m_Hunger(hunger)
{
cout << "A new critter has been born!" << endl;
}


I understand that this is a constructor definition, and passes an int to the variable m_Hunger, but I don't understand the ':'. Is this shorthand of some sort?

In previous examples in the book, the constructor definition looked like:

1
2
3
4
5
Critter::Critter(int hunger) // constructor definition
{
cout << "A new critter has been born!" << endl;
m_Hunger = hunger;
}


Any help would be appreciated.
Last edited on
This is called a "Member Initializer List" and it is a handy way to set values of member data: http://en.cppreference.com/w/cpp/language/initializer_list
This is just the initializer list, you use it whenever you need to initialize variables in the constructor.

 
Class() : num(1), db(3.5) {}


Last edited on
Thanks guys, that makes sense.
Topic archived. No new replies allowed.