8-bitButterfly
8-bitButterfly

Reputation: 203

Copying object inside the initialization list

How to change Axes (Axes const &crAxes) { *this = crAxes; } into Axes (Axes const &crAxes) : (*this)(crAxes) {}, so to copy the object inside the initialization list (before X, Y, and Z are initialized to their default value).

Changing form this:

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

    float   X;
    float   Y;
    float   Z;

};

Into something like this:

struct Axes
{

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

    float   X;
    float   Y;
    float   Z;

};

Upvotes: 0

Views: 45

Answers (1)

ForEveR
ForEveR

Reputation: 55897

You cannot do such thing in copy c-tor. Use simply

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

However, there is no need in copy c-tor here, since default-implemented copy c-tor will do same things (memberwise-copy).

Upvotes: 4

Related Questions