c++: can vector<Base> contain objects of type Derived?

后端 未结 3 916
轻奢々
轻奢々 2020-11-29 11:46

The title pretty much says it all. Basically, is it legal to do this:

class Base {
    //stuff
}

class Derived: public Base {
    //more stuff
}

vector<         


        
3条回答
  •  暖寄归人
    2020-11-29 11:54

    No, the Derived objects will be sliced: all additional members will be discarded.

    Instead of raw pointers, use std::vector >.

提交回复
热议问题