That a part of what's called the initialization list. It's just another way to initialize member variables in a class/struct constructor. It's also the only way to initialize a class/struct's const data members.
Instead of:
Code:
class foo
{
int a;
char b;
float c;
public:
foo()
{
a = 10;
b = 'z';
c = 8.9f;
}
};
You can do:
Code:
class foo
{
int a;
char b;
float c;
public:
foo() : a(10), b('z'), c(8.9f)
{
}
};
If your class had a const data member, there would be no way to initialize that member in the constructor body because by that time the object is already built and whatever value might be in that const data member is the value its going to have because it can't be changed. The only way to initialize those const data members is to use the initializer list.
You can't do this:
Code:
class foo
{
const int a;
public:
foo()
{
a = 10; // Won't work, a is const
}
};
You can however do this:
Code:
class foo
{
const int a;
public:
foo() : a(10) // Ok to do this
{
}
};