2010-10-24 141 views
12

比方说,我有一个类,并且用户可以创建。怎么做?我明白我的className objectName(args);创建对象,但是如何动态地做到这一点,取决于用户输入?如何动态创建类对象?

+3

你可以给一个(可能伪代码)的代码示例? – 2010-10-24 07:15:55

+0

创建它们在哪里?例如,你可以将它们存储在'std :: vector'中,但这取决于你在做什么。 – GManNickG 2010-10-24 07:17:17

+0

不幸的是,在C++中,你不能动态调用构造函数。唯一的方法是存储能够在运行时返回想要的新构造对象的对象。你已经收到的例子答案是完全相关的。 – 2012-03-01 15:07:37

回答

5

以下工厂方法创建动态地基于用户输入Box实例:

class BoxFactory 
{ 
    public: 
    static Box *newBox(const std::string &description) 
    { 
     if (description == "pretty big box") 
     return new PrettyBigBox; 
     if (description == "small box") 
     return new SmallBox; 
     return 0; 
    } 
}; 

当然,PrettyBigBoxSmallBox无论从Box派生。看看C++ design patterns wikibook中的创作模式,因为它们中的一个可能适用于您的问题。

2

在C++中,可以分配使用自动(栈)和动态(堆)存储的对象。

Type variable_name; // variable_name has "automatic" storage. 
        // it is a local variable and is created on the stack. 

Type* pointer_name = NULL; // pointer_name is a "pointer". The pointer, itself, 
          // is a local variable just like variable_name 
          // and is also created on the stack. Currently it 
          // points to NULL. 

pointer_name = new DerivedType; // (where DerivedType inherits from Type). Now 
           // pointer_name points to an object with 
           // "dynamic" storage that exists on the heap. 

delete pointer_name; // The object pointed-to is deallocated. 
pointer_name = NULL; // Resetting to NULL prevents dangling-pointer errors. 

您可以使用指针和堆分配到动态构造对象为:

#include <cstdlib> 
#include <iostream> 
#include <memory> 
class Base { 
    public: 
     virtual ~Base(){} 
     virtual void printMe() const = 0; 
    protected: 
     Base(){} 
}; 
class Alpha : public Base { 
    public: 
     Alpha() {} 
     virtual ~Alpha() {} 
     virtual void printMe() const { std::cout << "Alpha" << std::endl; } 
}; 
class Bravo : public Base { 
    public: 
     Bravo() {} 
     virtual ~Bravo() {} 
     virtual void printMe() const { std::cout << "Bravo" << std::endl; } 
}; 
int main(int argc, char* argv[]) { 
    std::auto_ptr<Base> pointer; // it is generally better to use boost::unique_ptr, 
           // but I'll use this in case you aren't familiar 
           // with Boost so you can get up and running. 
    std::string which; 
    std::cout << "Alpha or bravo?" << std::endl; 
    std::cin >> which; 
    if (which == "alpha") { 
     pointer.reset(new Alpha); 
    } else if (which == "bravo") { 
     pointer.reset(new Bravo); 
    } else { 
     std::cerr << "Must specify \"alpha\" or \"bravo\"" << std::endl; 
     std::exit(1); 
    } 
    pointer->printMe(); 
    return 0; 
} 

相关:the "Factory" object-oriented design pattern

15

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

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

我认为,在现实中,你必须对您要创建实例类的数量有限。所有其他答案都做出这个假设。你实际需要的是工厂模式。在接下来的代码中,我还以为它的要创建实例类,都来自同一个基类派生,让我们说动物,像这样:

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

然后创建一个抽象的工厂,是一个接口,创建一个动物:

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

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

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

祝你好运。

0

一个简单的方法是使用矢量。首先,包含矢量库并将临时对象作为您的类。

class temp; 

然后,使例如命名对象的向量与类类型:

#include <vector> 
. 
. 
vector <class>objects; 

,那么你可以将循环添加到添加object.for例子,我有一个类名为temp的有一个名为input的函数,我想补充:

while(1){ 
     temp.input(); 
     objects.push_back(temp); 
     } 

现在你有一个动态类。 来访问你的对象,你可以用这样的方式:

objects[i]; 

,如果你想删除一个对象,只是用这种方式: 1.find在矢量你的对象的位置。如果你想知道你的向量的最后一个块的位置做这个

objects[location of the object you want to remove]=objects[location of your last block]; 
objects.pop_back(); 

int lastblock; 
lastblock=(objects.size()-1); 
您的载体与最后一块的 2.change量和除去最后一个块

注意:你可以使用像数组这样的向量。