常引用型成员变量(P123)
/*
常引用型成员变量也必须在构造函数的初始化列表中进行初始化。
*/
#include<iostream>
using namespace std;
int fvalue = 10;
class CDemo
{
public:
const int num;
const int& ref;
int value;
public:
CDemo(int n):num(n),ref(value),value(4){}
};
int main()
{
cout<<sizeof(CDemo)<<endl;
CDemo f(100);
//f.ref = f.value; //错误
cout<<"f.num="<<f.num<<"\tf.ref="<<f.ref<<"\tf.value="<<f.value<<endl;
return 0;
}

浙公网安备 33010602011771号