Copy assignment operator of subclass not overwriting parent class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using namespace std;

struct A {
    void operator=(const A& rhs) {
        cout<<"A"<<endl;
    }
};

struct B : public A {
    void operator=(const A& rhs) {
        cout<<"B"<<endl;
    }
};

int main() {
    B b1;
    B b2;
    b1 = b2;
    return 0;
}


Why does this program call A's copy assignment operator instead of B's?
Last edited on
Because you didn't provide one, your B class gets a default copy assignment created for you, of this form:

void operator=(const B& rhs);

That function, created for you, calls the base copy assignment operator.
Topic archived. No new replies allowed.