Pel*_*a86 3 c++ initialization
是否可以初始化这样的类?
Quaternion::Quaternion(){ //default without arguments
Quaternion(0.,V3(0.,0.,0.));
}
Quaternion::Quaternion(double s, V3 v){ //with scalar and vector as a argument
coords[0] = s;
coords[1] = v[0];
coords[2] = v[1];
coords[3] = v[2];
}
Run Code Online (Sandbox Code Playgroud)
因为这是输出:
QUATERNION TEST
(2.122e-313:-3.22469e-232:2.122e-313:-1.998) //instanciated with Quaternion q;
(4:1:2:3) // instanciated with the Quaternion q(4,1,2,3);
(4:-1:-2:-3) // conjugated of the one above
Run Code Online (Sandbox Code Playgroud)
并且第一个没有初始化为0.因为它应该是......为什么?
Ker*_* SB 13
不是在C++ 03中,但最近的C++版本允许委托构造函数:
Quaternion() : Quaternion(0.0, V3(0.0,0.0,0.0)) { }
Run Code Online (Sandbox Code Playgroud)
在委托构造函数中,初始化列表必须只有一个元素,这是另一个构造函数(显然不能有任何循环引用).
如果你想直接在构造函数中初始化你的类成员,那么你不会真正解决复制/粘贴问题.也许您想使用默认参数?有些人反对那些......
explicit Quaternion(double s = 0.0, V3 v = V3(0.0,0.0,0.0)) { /* ... */ }
Run Code Online (Sandbox Code Playgroud)