c++ 初始化列表

class Teacher
{
public:
    Teacher(string name = "Jim", int age = 1, int m = 100);
    void setName(string name);
    string getName();
    void setAge(int age);
    int getAge();
    int getMax();
private:
    string m_strName;
    int m_iAge;
    const int m_iMax; //const 的值
};
Teacher::Teacher(string name, int age, int m):m_strName(name),m_iAge(age),m_iMax(m)
//初始化列表
{
    cout << "Teacher(string name, int age)" << endl;
}
int Teacher::getMax()
{
    return m_iMax;
}

初始化列表可以改变private里const 的值

int main()
{
    Teacher t1("Merry",12,150);
    cout << t1.getName() << " " << t1.getAge() <<" "<< t1.getMax() << endl;
    system("pause");
    return 0;
}

你可能感兴趣的:(c++ 初始化列表)