I have a simple struct Wrapper
, distinguished by two templated assignment operator overloads:
template<typename T>
struct Wrapper {
Wrapper() {}
template <typename U>
Wrapper &operator=(const Wrapper<U> &rhs) {
cout << "1" << endl;
return *this;
}
template <typename U>
Wrapper &operator=(Wrapper<U> &rhs) {
cout << "2" << endl;
return *this;
}
};
I then declare a and b:
Wrapper<float> a, b;
a = b;
assigning b
to a
will use the non-const templated assignment operator overload from above, and the number "2" is displayed.
What puzzles me is this: If I declare c
and d
,
Wrapper<float> c;
const Wrapper<float> d;
c = d;
and assign d
to c
, neither of the two assignment operator overloads is used, and no output is displayed; so the default copy assignment operator is invoked. Why does assigning d
to c
not use the const overloaded assignment operator provided? Or instead, why does assigning b
to a
not use the default copy assignment operator?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…