How to create class objects dynamically?

后端 未结 3 2094
不知归路
不知归路 2020-12-08 01:13

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

相关标签:
3条回答
  • 2020-12-08 01:51

    In C++, it is possible to allocate objects using automatic (stack) and dynamic (heap) storage.

    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.
    

    You can use pointers and heap-allocation to dynamically construct objects as in:

    #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;
    }
    

    Related: the "Factory" object-oriented design pattern

    0 讨论(0)
  • 2020-12-08 01:57

    The following factory method creates Box instances dynamically based on user input:

    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;
        }
    };
    

    Of course, PrettyBigBox and SmallBox both derive from Box. Have a look at the creational patterns in the C++ design patterns wikibook, as one of them probably applies to your problem.

    0 讨论(0)
  • 2020-12-08 02:03

    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.

    0 讨论(0)
提交回复
热议问题