Possible Duplicate:
What does a colon following a C++ constructor name do?
I found the example below online however the syntax for the constructor confuses me a little bit especially the : symbol. Could anyone please give me a brief explanation ? Thanks.
struct TestStruct {
int id;
TestStruct() : id(42)
{
}
};
The constructor initializes
idto42when it’s called. It’s called an initliazation list.In your example, it is equivalent to
You can do it with several members as well
It’s useful when your constructor’s only purpose is initializing member variables