如何动态创建类对象? [英] How to create class objects dynamically?

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

问题描述

假设我有一个类,用户可以创建。怎么做?我明白我通过 className objectName(args); 创建对象,但是如何根据用户输入动态执行?

解决方案

正确的答案取决于您要创建实例的不同类别的数量。



如果数字是巨大的(应用程序应该能够在应用程序中创建任何类的实例),您应该使用.Net的反射功能。但是,说实话,我不是在商业逻辑中使用反思的大粉丝,所以我建议不要这样做。



我认为在现实中你您要创建实例的类的数量有限。所有其他答案都是这样做的。您实际需要的是工厂模式。在下一个代码中,我也假设你想要创建实例的类都是从相同的基类派生出来的,就像Animal一样:

  class Animal {...}; 
class狗:public Animal {...}
class Cat:public Animal {...}

然后创建一个抽象工厂,它是一个创建动物的界面:

  class IFactory 
{
public:
Animal * create()= 0;
};

然后为每种不同类型的动物创建子类。例如。对于Dog类,这将成为:

  class DogFactory:public IFactory 
{
public:
Dog * create(){return new Dog();}
};

和猫一样。



DogFactory :: create方法会覆盖IFactory :: create方法,即使它们的返回类型不同。这就是所谓的共变型返回类型。只要子类方法的返回类型是基类的返回类型的子类,这是允许的。



现在可以做的是将所有的这些工厂在地图上,如下所示:

  typedef std :: map< char *,IFactory *> AnimalFactories 
AnimalFactories animalFactories;
animalFactories [Dog] = new DogFactory();
animalFactories [Cat] = new CatFactory();

用户输入后,您必须找到正确的工厂,并要求它创建动物:

  AnimalFactories :: const_iterator it = animalFactories.find(userinput); 
if(it!= animalFactories.end())
{
IFactory * factory = * it;
Animal * animal = factory-> create();
...
}

这是典型的抽象工厂方法。
还有其他方法。当我自己教C ++时,我写了一篇关于它的小型CodeProject文章。你可以在这里找到: http://www.codeproject.com/KB/architecture/all_kinds_of_factories .aspx



祝你好运。


Let's say I have a class box, and an user can create boxes. How to do it? I understand I create objects by className objectName(args); but how to do it dynamically, depending on the user input?

解决方案

The correct answer depends on the number of different classes of which you want to create the instances.

If the number is huge (the application should be able to create an instance of any class in your application), you should use the reflection functionality of .Net. But, to be honest, I'm not a big fan of using reflection in business logic, so I would advise not to do this.

I think that in reality you have a limited number on classes for which you want to create instances. And all the other answers make this assumption. What you actually need is a factory pattern. In the next code I also assume that the classes of which you want to create instances, all derive from the same base class, let's say Animal, like this:

class Animal {...};
class Dog : public Animal {...}
class Cat : public Animal {...}

Then create an abstract factory which is an interface that creates an animal:

class IFactory
   {
   public:
      Animal *create() = 0;
   };

Then create subclasses for each of the different kinds of animals. E.g. for the Dog class this will become this:

class DogFactory : public IFactory
   {
   public:
      Dog *create() {return new Dog();}
   };

And the same for the cat.

The DogFactory::create method overrules the IFactory::create method, even if their return type is different. This is what is called co-variant return types. This is allowed as long as the return type of the subclass's method is a subclass of the return type of the base class.

What you can now do is put instances of all these factories in a map, like this:

typedef std::map<char *,IFactory *> AnimalFactories
AnimalFactories animalFactories;
animalFactories["Dog"] = new DogFactory();
animalFactories["Cat"] = new CatFactory();

After the user input, you have to find the correct factory, and ask it to create the instance of the animal:

AnimalFactories::const_iterator it=animalFactories.find(userinput);
if (it!=animalFactories.end())
   {
   IFactory *factory = *it;
   Animal *animal = factory->create();
   ...
   }

This is the typical abstract factory approach. There are other approaches as well. When teaching myself C++ I wrote a small CodeProject article about it. You can find it here: http://www.codeproject.com/KB/architecture/all_kinds_of_factories.aspx.

Good luck.

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

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