首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

关于参数初始化列表和引用的有关问题

2012-11-09 
关于参数初始化列表和引用的问题#include iostreamusing namespace stdclass A{private:const int i,j

关于参数初始化列表和引用的问题
#include <iostream>
using namespace std;
class A
{
private:
const int i,j;
int &k;
public:
A(int a,int b,int x):i(a),j(b),k(x)
{
cout<<"i="<<i<<"\t"<<"j="<<j<<"\t"<<"k="<<k<<endl;
}
void output()
{
cout<<k<<endl;
}
};
void main()
{
int m=6;
A x(4,5,m);
x.output();//调用k的应用,值为随机???
}

问题是x.output()输出的是4199863,应该是一个随机值。而构造函数中输出的k值没有问题。
请大虾们帮助。

[解决办法]
A(int a,int b,int& x):i(a),j(b),k(x)
{
cout<<"i="<<i<<"\t"<<"j="<<j<<"\t"<<"k="<<k<<endl;
}

热点排行