Adrian wrote:
Ondra Holub wrote:
>In container::operator[] you are returning reference to base. But there
is no assignment operator in class 'base', so compiler cannot see it.
Should be. The compiler should generate one by default.
Surely. base& base::operator=(const base&) is declared by default. Remarks:
1. It does not match the signature of deriv<T>& deriv<T>::operator=(const
T&)
2. It is not virtual.
1 Means that when the comiler searches base (static type of lhs in
assignment) for operator=, it cannot see any operator= taking a T, even
though the dynamic type of lhs has one.
2 Means that even if the signatures had matched, the compiler would
statically link to base::operator=. Example of latter:
#include <iostream>
using namespace std;
struct foo {
virtual foo& operator =(const foo&) {
cout << "foo\n";
return *this;
}
};
struct bar : foo {
bar& operator=(const foo&) {
cout << "bar\n";
return *this;
}
};
int main() {
foo f;
bar b;
foo & fb = b;
f = f;
b = b;
fb = b;
}
Delete the virtual kw in foo, and see how output changes.
--
Robert Bauck Hamar
Der er to regler for suksess:
1. Fortell aldri alt du vet.
- Roger H. Lincoln