Reputation: 117
I'm sure this has been answered many times, but I don't know how to search for this. Is this polymorphism? Overloading?
Basically I want to take a vector, and modify its functions the way I want them or create new ones.
E.g: I want push_back() to insert an element and keep the vector in descending order. So the addition would be to swap() the element to its respective place. Or I would like to add a new function pop_front().
That being said, I need it as a member function, e.g:
vector<int> x;
x.pop_front();
not:
pop_front(x);
Is this ever done in practice? I know I can just use existing containers, like a priority queue for my example, but I'd rather make completely custom functions.
Upvotes: 1
Views: 65
Reputation: 172894
Basically, std::vector
is not designed to be base class, inherit from it would be a bad idea in general. For example its dtor is not virtual.
It's better to use composition here. e.g.
template <typename T>
class MyVector {
private:
std::vector<T> v;
public:
void pop_front() {
// processing on v ...
}
};
Upvotes: 1