I have a class called Path that extends std::vector<Square *>, where Square is also a class that I have created. The Path will serve as guide for an Entity traversing a 2D environment. I have need of getting the longest path & shortest path acheivable, and therefore I am looking to find the number of squares between two Squares in a Path. To do this, I feel that it would be beneficial to overload, std::vector<Square *>::push_back(const value_type &__x), although I am not sure what the syntax for that would be. I am currently trying this:
class Path : public std::vector<Square *>
{ //... functional stuff, not relevant.
int length;
public:
push_back(const value_type &__x)
{ Square *last_square = this->at(this->size() - 1);
// how do I call super class push_back?
// however that works, I push back &__x square here.
Square *most_recent = (Square *)&__x;
int delta_x = compare_distance(last_square, most_recent);
length += delta_x;
};
int path_length() { return length; };
};
Of course, I suppose I could just write a method inwhich I call push_back for the super class, but I feel like it is more concise to override the function, plus it is good practice for me to learn how to properly override stl functions.
Thanks!
Actually, it’s not.
STL containers are not meant to be inherited (no
virtualmethod), therefore good practice is to use composition and provide a meaningful interface to your class that use the STL container methods to accomplish its goal.