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;
}