Possible Duplicate:
What is this weird colon-member syntax in the constructor?
So I was looking through some samples in a Direct X 10 book, when I came across this
PeaksAndValleys::PeaksAndValleys()
: mNumRows(0), mNumCols(0), mNumVertices(0), mNumFaces(0),
md3dDevice(0), mVB(0), mIB(0)
{
}
I understand this is probably supposed to be a constructor……but i’ve never seen one like this? can someone explain? specifically why is there a single : after it’s declared?
This is called an initialization list, and is used for initializing class members. It is useful both as a shorthand and it is required for initializing members that do not have a default constructor. For example:
This gives an error because
membercannot be default constructed, asFoodoes not have a default constructor. Change it toand now it works.
This syntax is also useful for initializing
constmembers of a class, as while they might be default-constructible, you cannot overwrite them in the constructor body.The same idea also applies to references, as they also must be initialized directly. Finally, it is used for specifying arguments for the base class constructor.