Possible Duplicate:
What is this weird colon-member syntax in the constructor?
I’m trying to understand what this kind of code means
Say I have this
class OptionStudent: public Student // derived class from Student class
{
public:
explicit OptionStudent(const std::string id = "12345678",
const std::string first = "someone")
: Student(id, first)
{
count_++;
}
}
What is that colon after the “someone”): <– part called or mean for this constructor?
I know the constructor may be a little incorrect but I don’t know what this is called. I just copied my notes from what the instructor was writing on the board and didn’t understand it.
Something to do with the class or object remembering something?
It is the member initialization list. In this case, it calls the base class’s constructor with
idandfirstas arguments. It could also provide initial values for non-staticdata members of your class (if you had any).Note that the semicolon after
Student(id, first);is a syntax error and needs to be removed.