class A, B;
class A {
public:
A& operator= ( const A &rhs ) { return *this; }
};
class B: public A {
public:
B& operator= ( const A &rhs ) { return *this; }
};
A a;
B b;
std::list < A > aa;
std::list < B > bb;
a = b; // works
b = a; // works
// aa = bb; // fails
// bb = aa; // fails
How do I get bb = aa to work?
What you're missing here is that even though A
and B
are related types, std::list<A>
and std::list<B>
are actually unrelated types (other than both saying list
). As such you can't use copy assignment to assign between them.
However, assuming that you're ok assigning the types A
and B
to each other using their assignment operators, you can use list
's assign
method to copy the range of iterators:
aa.assign(bb.begin(), bb.end());
bb.assign(aa.begin(), aa.end());