const对象或引用只能初始化但是不能赋值。构造函数的函数体内只能做赋值而不是初始化,因此初始化const对象或引用的唯一机会是构造函数函数体之前的初始化列表中。
2016-06-01
#include <iostream>
#include <string>
using namespace std;
class Student{
public:
string m_strName;
int m_iAge;
};
int main()
{
Student stu;
stu.m_strName = "慕课网";
stu.m_iAge = 2;
cout << stu.m_strName << " " << stu.m_iAge<< endl;
return 0;
}
#include <string>
using namespace std;
class Student{
public:
string m_strName;
int m_iAge;
};
int main()
{
Student stu;
stu.m_strName = "慕课网";
stu.m_iAge = 2;
cout << stu.m_strName << " " << stu.m_iAge<< endl;
return 0;
}