常引用型成员变量(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;
}

posted @ 2020-03-09 17:25  CollisionDimension  阅读(185)  评论(0)    收藏  举报