Right design pattern to deal with polymorphic collections of objects

后端 未结 5 1047
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-22 04:59

Suppose I have the following classes:

class BaseObject {
    public:
        virtual int getSomeCommonProperty();
};

class Object1: public BaseObject {
    publ         


        
5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-22 05:46

    I think you should go for option 1 but use a static cast instead. After all the derived collection knows the type of the member variable for sure.

    This answer explains it very well.

提交回复
热议问题