This seems like a pretty basic problem, but I can't figure it out. I have a std::vector
of raw pointers to Derived objects, and I just want to copy it to another vector of Base pointers using the assignment operator. With VC++ I get error C2679 "binary '=': no operator found..." BTW I don't want a deep copy of the objects, I just want to copy the pointers. Sample code:
#include <vector>
using namespace std;
struct Base{};
struct Derived: public Base {};
int main (int argc, char* argv[])
{
vector<Derived*> V1;
vector<Base*> V2;
V2 = V1; //Compiler error here
return 0;
}
What confuses me is that I can copy the vector by looping through it and using push_back
, like this:
for (Derived* p_derived : V1)
V2.push_back(p_derived);
So my question is why does the assignment fail, while push_back
works? Seems like the same thing to me.