2013-06-04 33 views
0

如何將Axes (Axes const &crAxes) { *this = crAxes; }更改爲Axes (Axes const &crAxes) : (*this)(crAxes) {},以便將初始化列表中的對象(在將X,Y和Z初始化爲其默認值之前)複製到初始化列表中。在初始化列表中複製對象

改變形式是:

struct Axes 
{ 
    Axes() : X(0.f), Y(0.f), Z(0.f) {} 
    Axes (Axes const &crAxes) { *this = crAxes; } 

    float X; 
    float Y; 
    float Z; 

}; 

弄成這個樣子:

struct Axes 
{ 

    Axes() : X(0.f), Y(0.f), Z(0.f) {} 
    Axes (Axes const &crAxes) : (*this)(crAxes) {} 

    float X; 
    float Y; 
    float Z; 

}; 

回答

4

你不能做這樣的事情在副本C-TOR。 使用簡單

Axes(const Axes& rhs) : X(rhs.X), Y(rhs.Y), Z(rhs.Z) {} 

然而,在複製C-TOR沒有必要在這裏,因爲默認情況下實現的複製C-TOR會做同樣的事情(memberwise-copy)。

+0

剛剛完成關於「成員副本」和「淺拷貝,深拷貝,按位拷貝」的研究。謝謝你的提示。 ^^ –