|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
class point
{
private:
int xpos;
int ypos;
int &ref1;
double &ref2;
public:
point(int x,int y,double &z):ref1(xpos),ref2(z)//此时xpos不是尚未初始化么,那怎么给ref1赋值?(如果ref1不是引用变量,那这样写就是不对的,我知道为什么,但是为什么ref1是引用变量,那这样写就对的,我不知道为什)
{
xpos=x;
ypos=y;
}
point(const point &pt):ref1(pt.ref1),ref2(pt.ref2)
{
xpos=pt.xpos;
ypos=pt.ypos;
}
void print()
{
cout<<"xpos:"<<xpos<<"ypos:"<<ypos<<endl;
cout<<"ref1:"<<ref1<<"ref2:"<<ref2<<endl;
}
void setx(int x)
{
xpos=x;
}
};
int main()
{
double outint=5.0;
int wa=1;
point p1(3,4,outint);
p1.print();
return 0;
}
课本上说初始化顺序是按变量声明的顺序,又说参数初始化表的顺序先于构造函数函数体执行,那
point(int x,int y,double &z):ref1(xpos),ref2(z)
{
xpos=x;
ypos=y;
}
的顺序不应该是ref1,ref2,xpos,ypos么,可事实上为什么:ref1(xpos),是对的,问什么?
|
|