Is it okay to inherit implementation from STL containers, rather than delegate?

前端 未结 7 982
一生所求
一生所求 2020-11-22 07:15

I have a class that adapts std::vector to model a container of domain-specific objects. I want to expose most of the std::vector API to the user, so that they may use famili

7条回答
  •  南笙
    南笙 (楼主)
    2020-11-22 07:51

    Virtual dtors aside, the decision to inherit versus contain should be a design decision based the class you are creating. You should never inherit container functionality just because its easier than containing a container and adding a few add and remove functions that seem like simplistic wrappers unless you can definitively say that the class you are creating is a kind-of the container. For instance, a classroom class will often contain student objects, but a classroom isn't a kind of list of students for most purposes, so you shouldn't be inheriting from list.

提交回复
热议问题