我有两个继承相同抽象基类的类:
class base
{ virtual void something() = 0; };
class a : public base
{
void something();
};
class b : public base
{
void something();
// This is what I want: a list where I can store values of type a
// and of type b
// std::list<a & b> objs;
};
我可以使用原始/智能指针列表 ( list<base*> obj_ptrs
),但如何使用此列表?
b b_obj;
b_obj.obj_ptrs.push_back(new a());
// Who have to delete this pointer? Who use the class or who design
// the class in the object destructor?
// The following is valid for c++11
auto p = b_obj.obj_ptrs.back();
// But if i'm using c++03?
我希望使用该课程的人有可能这样做:
a a_obj;
b b_obj;
b_obj.obj_ptrs.push_back(a);
b_obj.obj_ptrs.push_back(b);
我该如何设计我的课程来完成这项工作?