C++ does not support polymorphic copy or move assignment operators. For example, the signature of a copy assignment operator on a "Base" class
would be Base& operator=(const Base& other)
.
And on a "Derived" class that extends "Base", it would be Derived& operator=(const Derived& other)
.
Because these are two entirely different method signatures, the second method does not override the first, and adding virtual
to the
"Base" signature does not change which method is called.
It is possible to add an operator=
override in a derived class, but doing so is an indication that you may need to reexamine your
application architecture.
Noncompliant code example
class Base {
public:
virtual Base& operator=(const Base& other); // Noncompliant
};
class Derived : public Base {
public:
Derived& operator=(const Derived& other);
};
Compliant solution
class Base {
protected:
Base& operator=(const Base& other); // not virtual
};
class Derived : public Base {
public:
Derived& operator=(const Derived& other);
};