I have a base class A and two derived classes B and C. B defines the = operator, taking the base class A as parameter.
When calling = on class B, sometimes the operator of base class A is called instead of the one from B.
class A {
public:
void operator=(A &) {
printf("A =\n");
};
};
class B : public A {
public:
void operator=(A &s) {
printf("B =\n");
};
};
class C : public A {
};
int main()
{
B b1, b2;
C c;
b1 = b2;
b1 = c;
}
Output is:
A =
B =
Why is the first assignment not calling B::operator=()?
Why is the second assignment not calling A::operator=() as well, as it is also derived from A?
What can i do to make B::operator=() be called every time?
I was totally surprised when i saw this. I noticed it only because i deleted operator=() ("operator=() = delete") in class A, leading to a compiler error.
B::operator=
supposed to take anA
instead of aB
? – Hydrophone