如何设计我的课? [英] How to design my class?

查看:160
本文介绍了如何设计我的课?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类继承相同的抽象基类:

I have two class that inherit the same abstract base class:

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& > obj_ptrs ),但如何使用此列表?

I could use a list of raw/smart pointers (list<base*> obj_ptrs), but how to use this list?

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?

我想让使用类的人有这样做的可能性:

I want that who use the class have the possibility to do this:

a a_obj;
b b_obj;

b_obj.obj_ptrs.push_back(a);
b_obj.obj_ptrs.push_back(b);

我如何设计我的类来完成这项工作?

How have I to design my classes to do this work?

推荐答案


我可以使用一个原始/智能指针列表( list< base *> obj_ptrs ),但是如何使用此列表?

I could use a list of raw/smart pointers (list<base*> obj_ptrs), but how to use this list?

您将通过调用虚函数情况下,使用 dynamic_cast 将指针类型转换为 a * b *

You would interact with them polymorphically by calling virtual functions or, in exceptional circumstances, using dynamic_cast to convert the pointer type to a* or b*.


谁必须删除此指针吗?

Who have to delete this pointer?

您必须决定所有权计划。一种方法是在列表中存储智能指针(通常 unique_ptr ,或者在特殊情况下可能 shared_ptr ),然后它们会在从列表中删除时自动删除。另一种方法是从列表中分别管理对象,只是将原始指针放在列表中。

You will have to decide on the ownership scheme. One approach is to store smart pointers (usually unique_ptr, or perhaps shared_ptr in special circumstances) in the list, and then they'll be automatically deleted on removal from the list. Another approach is to manage the objects separately from the list, and just put raw pointers in the list.

// The following is valid for c++11
auto p = b_obj.obj_ptrs.back();
// But if i'm using c++03?

这相当于 base * p = b_obj.obj_ptrs.back auto 不会神奇地给出动态指针类型,它只是静态指针类型的快捷方式。

That's equivalent to base * p = b_obj.obj_ptrs.back();. auto will not magically give the dynamic pointer type, it's just a shortcut for the static pointer type.


我如何设计我的类来完成这项工作? (存储不同类型的对象)

How have I to design my classes to do this work? (storing objects of different types)

存储不同类型的对象可以使用基本上,具有一些额外元数据的联合,以跟踪哪个联合成员是活动的。这些在C ++中实现起来相当棘手,但您可以使用 Boost.Variant Boost.Any 。这有一个优点,你不需要一个公共的基类,或任何其他支持从存储的类型;可以使用任何一组(非抽象)类型。

Storing objects of different types can be done using a discriminated union - basically, a union with some extra metadata to keep track of which union member is active. These are quite tricky to implement in C++, but you could use Boost.Variant or Boost.Any. This has the advantage that you don't need a common base class, or any other support from the types being stored; any set of (non-abstract) types can be used.

这篇关于如何设计我的课?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆