I'm studying C++ and I have a question regarding assignment operators.
Based on what is written here https://en.cppreference.com/w/cpp/language/copy_assignment, it seems that
...a class can have multiple copy assignment operators, e.g. both T& T::operator=(const T &) and T& T::operator=(T).
I tried to make a class with both operators but I don't see where I'm wrong, because I get the this from the compiler:
error C2593: 'operator =' ambiguous*
This is the class:
class Point2D
{
public:
Point2D(); // default constructor
Point2D(double xValue, double yValue); // overloaded constructor
Point2D(const Point2D& ref); // copy constructor const
Point2D(Point2D& ref); // copy constructor for copy and swap
Point2D(Point2D&& moveRef); // move constructor
~Point2D(); // destructor
Point2D& operator=( const Point2D& other ); // copy assignment operator const
Point2D& operator=( Point2D other ); // copy assignment operator for copyAndSwap
private:
double x;
double y;
int *ptr;
};
This is where it gives me the error:
void copy_assign_test()
{
cout << endl << "Copy assign" << endl;
Point2D a(1, 1);
Point2D b(2, 2);
Point2D c(3, 3);
Point2D& ptRef = c;
Point2D d(6, 6);
a = ptRef; // error: ambiguous operator
b = a; // // error: ambiguous operator
d = Point2D(7,7); // error: ambiguous operator
}
My question concerns the following:
- Why is
a=ptRefambiguous ifptRefis a reference? - Why is
b=ais ambiguous ifaisn't declared as reference? - Why is
d = Point2D(7,7)ambiguous ifPoint2D(7,7)isn't a reference?
All my tests compiled using Visual Studio 2019, with the C++17 standard.