tags:

views:

100

answers:

4

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?

A: 

Well, depending on the language (looks like Java?) Your best bet will be to implement a listener method to pick up when the user (for instance) presses a button, and then within that method you can create and place a new box, remember to repaint your GUI and if the method does any heavy lifting you'll want to put it in a new thread.

EDIT: sorry, I just noticed this was tagged at c++, same idea though.

OmnipotentEntity
That's quite a narrow targeted answer for such a vague question.
PigBen
A: 

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.

Ton van den Heuvel
A: 

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;
}
Michael Aaron Safyan
A: 

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.

Patrick