#include <iostream>
using namespace std;
class Test
{
int x,y;
public:
Test(int val):y(val),x(y){}
int get_x()const
{
return x;
}
int get_y()const
{
return y;
}
};
void main ()
{
Test test(9);
cout<<test.get_x()<<" "<<test.get_y()<<endl;
}
上面是我写的一个测试代码,下面是c++ primer 4th edition 上的关于相关内容的描述
class X {
int i;
int j;
public:
// run-time error: i is initialized before j
X(int val): j(val), i(j) { }
};
In this case, the constructor initializer is written to make it appear as if j is initialized with val
and then j is used to initialize i . However, i is initialized first. The effect of this initializer is to
initialize i with the as yet uninitialized value of j !
=============================================================================================================
但是经过我的调试,初始化了的不是x,而是y(当然我想在说的是就我写的那个测试代码来说的),调试的结果跟书上说的那个貌似有点不一样了,知道是怎么回事的大牛们来解答!谢谢