从动态类型信息创建一个新对象 [英] Creating a new object from dynamic type info

查看:177
本文介绍了从动态类型信息创建一个新对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,是否有任何方法来查询对象的类型,然后使用该信息动态创建相同类型的新对象?

In C++, is there any way to query the type of an object and then use that information to dynamically create a new object of the same type?

,说我有一个简单的3类层次结构:

For example, say I have a simple 3 class hierarchy:

class Base
class Foo : public Base
class Bar : public Base

现在假设我给你一个类型为Base的对象 - 类型Foo。
有没有办法查询类型并使用该信息以后创建类型为Foo的新对象?

Now suppose I give you an object cast as type Base -- which is in reality of type Foo. Is there a way to query the type and use that info to later create new objects of type Foo?

推荐答案

克隆方法



查询类型的语言没有提供任何内容,您可以从该信息构造,但您可以通过各种方式为您的类别提供能力,最简单的是使用一个虚方法:

Clone method

There is nothing provided by the language that queries type and lets you construct from that information, but you can provide the capability for your class heirarchy in various ways, the easiest of which is to use a virtual method:

struct Base {
  virtual ~Base();
  virtual std::auto_ptr<Base> clone(/*desired parameters, if any*/) const = 0;
};

这有些不同:克隆当前对象。这通常是你想要的,并允许你把对象作为模板,然后你可以根据需要克隆和修改。

This does something slightly different: clone the current object. This is often what you want, and allows you to keep objects around as templates, which you then clone and modify as desired.

展开 Tronic ,您甚至可以生成 克隆功能

为什么选择auto_ptr ?因此,您可以使用分配对象,明确转让所有权,调用者不必怀疑 delete 必须释放它。例如:

Why auto_ptr? So you can use new to allocate the object, make the transfer of ownership explicit, and the caller has no doubt that delete must deallocate it. For example:

Base& obj = *ptr_to_some_derived;
{ // since you can get a raw pointer, you have not committed to anything
  // except that you might have to type ".release()"
  Base* must_free_me = obj.clone().release();
  delete must_free_me;
}
{ // smart pointer types can automatically work with auto_ptr
  // (of course not all do, you can still use release() for them)
  boost::shared_ptr<Base> p1 (obj.clone());
  auto_ptr<Base>          p2 (obj.clone());
  other_smart_ptr<Base>   p3 (obj.clone().release());
}
{ // automatically clean up temporary clones
  // not needed often, but impossible without returning a smart pointer
  obj.clone()->do_something();
}



对象工厂



如果你喜欢完全按照你的要求,得到一个可以独立使用实例的工厂:

Object factory

If you'd prefer to do exactly as you asked and get a factory that can be used independently of instances:

struct Factory {}; // give this type an ability to make your objects

struct Base {
  virtual ~Base();
  virtual Factory get_factory() const = 0; // implement in each derived class
    // to return a factory that can make the derived class
    // you may want to use a return type of std::auto_ptr<Factory> too, and
    // then use Factory as a base class
};

许多相同的逻辑和功能可用于克隆方法,如 get_factory

Much of the same logic and functionality can be used as for a clone method, as get_factory fulfills half of the same role, and the return type (and its meaning) is the only difference.

我还覆盖了工厂a 情侣 。您可以调整我的 SimpleFactory类,以便您的工厂对象(由 get_factory )持有对全局工厂的引用以及要传递给创建的参数(例如类的注册名称—考虑如何应用boost :: function :: bind ,以方便使用)。

I've also covered factories a couple times already. You could adapt my SimpleFactory class so your factory object (returned by get_factory) held a reference to a global factory plus the parameters to pass to create (e.g. the class's registered name—consider how to apply boost::function and boost::bind to make this easy to use).

这篇关于从动态类型信息创建一个新对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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