Inheriting classes from std::

后端 未结 4 1574
甜味超标
甜味超标 2020-12-22 05:41

There are many similar questions and I found both pro and against reasons to use this pattern so I am asking this here:

I need to make a JSON implementation in C++ (

4条回答
  •  北海茫月
    2020-12-22 06:10

    • I wouldn't recommend to inherit from STL.

    • Due to performance reasons STL standard containers do not have virtual destructors, thus you cannot handle them polymorphically.

    • This means that there's no way to use runtime polymorphisim and expect proper desctructors for them.

    • Inheriting from STL, although is perfectly allowable, most of the times denotes bad design. I would recommend not following the inherits from way but rather has a way:


    namespace JSON {
      class JSON { };
      class object : public JSON 
      { 
        std::unordered_map m;
    
        public:
        // provide interface to access m.
        };
      class Vector : public JSON { 
        std::vector v;
    
        public:
        // provide interface to access v.
      };
      ...
    };
    

提交回复
热议问题