Best Practice For List of Polymorphic Objects in C++

前端 未结 4 1839
执念已碎
执念已碎 2020-12-19 11:25

What is a common practice for the storage of a list of base class pointers each of which can describe a polymorphic derived class?

To elaborate and in the interest o

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-19 12:04

    You could avoid lots of repetition in GenericShape by using templates (for the constructors and converters), but the key bit that's missing is having it inherit from Shape and implement its virtuals -- without it it's unusable, with it it's a pretty normal variant on envelope/implementation idioms.

    You may want to use auto_ptr (or somewhat-smarter pointers) rather than a bare pointer to Shape, too;-).

提交回复
热议问题