I have a class Step derived from std::vector<unsigned int>. I need to overload assignment operator because of the deep copy used in assignment of a value returned from a static method. I can’t figur out how should I copy all elements of rhv to this in assignment:
class Step : public std::vector<unsigned int>
{
public:
friend std::ostream& operator<<(std::ostream& outStream, const Step& step);
Step& operator =(const Step& rhv);
static Step fromString(const std::string &input);
// Something like: Step x = Step::fromString("12 13 14 15 16");
private:
double time;
double pause;
unsigned int id;
std::string name;
};
and then overloading =:
Step& Step::operator =(const Step& rhv)
{
time = rhv.time;
pause = rhv.pause;
id = rhv.id;
// How should I copy contents of rhv to `this` safely?
return *this;
}
I’m not 100% sure from your question, but I think you are asking about calling the parent
operator=. In that case you have two options:Of course in the code you have shown us you don’t do any manual resource handling, so I don’t see why you want to write your own assignment operator, the compiler generated one should do fine. Besides if you write your own assignment operator you might want to head the rule of three and write your own copy constructor and destructor too (at least in C++03, C++11 can be a bit different due to movable but not copyable classes).
As another sidenote: Most standardlibrary classes are not designed to be derived from, so you might want to rethink your design requiering you to inherit form
std::vector