1

Hi all I have a question regarding inheritence.

Before I start: I know that a lot of people do not like the idea of inheriting from vector, but for my education lets just assume I have to do it! Also, I am new to the code game as I'm sure you can tell by the question.

I've looked and looked and I just want to understand the basic use of a class inherited by vector. Such as

class MyClass : public vector<int> {
    public:
    MyClass(); //as a constructor
    ...other members
};

So i guess my questions are how can I access the vector functions now inherited by this class? I should not have to make a vector a member variable right? And how to properly implement the vector abilities the class now has when coding the details of the member function's?

Thanks in advance and also apologies in advance if this is unworthy.

Moop
  • 91
  • 5
  • 1
    You do not need a vector member variable. You need to understand the "is-a" vs "has-a" concept, your class *is a* type of vector, it does not *have a* vector. So your class, from inheritance, will have `begin`, `end`, `front`, `back`, `push_back`, etc functions all defined already for you. – Cory Kramer Dec 04 '14 at 18:27
  • 1
    Don't inherit standard container classes, use them as members! – πάντα ῥεῖ Dec 04 '14 at 18:27
  • @πάνταῥεῖ - why shouldn't he do that? – KCH Dec 04 '14 at 18:28
  • `I know that a lot of people do not like the idea of inheriting from vector`. And for good reason. `std::vector` does not have a virtual destructor and you should *never* inherit a class without a virtual destructor. – Bart van Nierop Dec 04 '14 at 18:28
  • @KCH It usually indicates bad design, and gives you a ton of problems to deal with. Usually it's not worth the effort, but can be done in much simpler ways. There are rare cases you need to specialize standard containers, or really inheriting them (as template class of course then). – πάντα ῥεῖ Dec 04 '14 at 18:31
  • @Cyber Thank you. I understand that the class is of a vector datatype, but could you provide an example of how I would say store data in it/utilize the vector functions when I code a member function. – Moop Dec 04 '14 at 19:58

0 Answers0