Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inheriting from std::vector

Tags:

c++

There are many answers on here saying not to inherit from std::vector and alike such as this question. I understand the reasons and agree with them. However in here Section 4.4.1.2 Bjarne Stroustrup himself inherits from std::vector to add range checking.

Is that a special case, or just something that's ok in that context or something that he really ought not be doing :P

like image 994
jcoder Avatar asked Apr 24 '13 09:04

jcoder


1 Answers

I think this answer perfectly answers your question.

It's not impossible to inherit from std::vector, it just probably would be very limited (due to no virtual destructor), quite confusing to others and extending by composition would be better/easier/more maintainable than inheritance anyway.

Perhaps Stroustrup simply wanted to show it's doable, but not necessarily to imply that he suggests it.

like image 195
Shahbaz Avatar answered Sep 18 '22 15:09

Shahbaz