I checked the operator overload in C ++ and came across something I did not expect, and I have some doubts.
My copy constructor is declared and implemented as
explicit Vector(const Vector& v); Vector::Vector(const Vector& v) : _x(v._x), _y(v._y), _z(v._z) {}
then I overload compound assignment operators
Vector Vector::operator+(const Vector& v) const { Vector tmp(*this); tmp += v; return tmp; } Vector Vector::operator-(const Vector& v) const { Vector tmp(*this); tmp -= v; return tmp; }
however, in the return I received the error message no matching constructor for initialization of 'Vector' .
Since the only thing I added to my constructor is the explicit keyword, I deleted it and the code compiles just fine, why?
I also tested new things from C ++ 11 and found that I could declare my constructor as a moving constructor
explicit Vector(const Vector&& v);
and the code compiles just fine. If I do this, do I need to have both copy and move constructors?
explicit Vector(const Vector& v); explicit Vector(const Vector&& v);
or just having a move constructor will work fine? If I want to stick with C ++ 11, what is the right approach?
source share