Thread: How to have a user create a new object/instance of a class

  1. #1
    Registered User
    Join Date
    Jan 2010

    How to have a user create a new object/instance of a class

    Hi, I apologise if this is a common question, or if I am going in the wrong direction entirely.

    I have been trying to get a better understanding of classes. What I haven't been able to work out is how to let the user create a new instance (I think I am asking the right question).

    For example: I have a class called Unit, and several classes that inherit from Unit - Car, Soldier, Robot etc. If I want to create a new car unit in my code, I would simply do

    Car CarOne;
    Is that correct so far?

    If I wanted to make an RTS game, or even a simple phonebook application this wouldn't work (or would it?). I need to have a function (do you use the constructor?) along the lines of "create new unit of type X" or "create new car".

    Is it simpler than I think? I hope so. Perhaps everytime you call the constructor it makes a seperate one, and I just have to initialise it with a unique ID number or similar?

    I hope I have made sense. Any help/tips/links would be greatly appreciated, I have been at this for days now.


  2. #2
    Registered User
    Join Date
    Jun 2005
    Evey object created by code is distinct and unique (unique address in memory, etc). The statement "Car CarOne;" creates an object of type Car, named CarOne. The constructor is invoked in that process at run time.

    However, a constructor is not linked to any particular object. If you create n cars (n an integer) then the constructor is invoked separately to initialise each one. For example, if you do
    //   definition of the class Car
    void some_function()
        Car CarOne;
        Car CarTwo;
           // code here can do things with CarOne and CarTwo
    a Car named CareOne is created (the constructor is invoked for it) then a Car named CarTwo (the constructor is invoked for it) whenever some code calls some_function(). Those two Car's continue to exist until some_function() returns. When some_function() returns, CarTwo is destroyed and then CarOne is destroyed (and the process of destroying each object invokes the destructor for that object).

    If the user someone wants to arbitrarily say "create new unit of type X" then you need some code that interprets that instruction, and dynamically creates an object of the correct type. There are many ways of doing that. A simple way is, assuming you have the user input stored in an array of char, is.
    #include <cstring>
    Unit *Create_Unit(const char *user_input)
         if (std::strcmp(user_input, "Car") == 0)
            return new Car;
         else if (std::strcmp(user_input, "Robot") == 0)
            return new Robot;
         // etc
    The problem with this is that the caller must remember to delete the returned object when finished with it, or there will be a memory leak. The other problem is that the function above must be manually modified to specifically support every type of object needed - which is cumbersome and error prone when you have lots of object types.

    There are more advanced techniques (eg look up the factory pattern) that take a bit more work to set up initially, but are easier to maintain in the long run.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    You can either use the factory pattern or the abstract factory pattern to accomplish what you want. There are several variations of both of these patterns as well. Pick which one best meets your design goals and requirements.

    Pointer management can be handed off to a third party library such as boost or you can do some simple reference counting. I highly recommend using boost.

  4. #4
    Registered User
    Join Date
    Jan 2010
    Thanks guys, I'm starting to understand a bit more. Going to look up factory patterns now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Create instance on class programatically
    By deviousdexter in forum C# Programming
    Replies: 6
    Last Post: 12-10-2008, 06:02 PM
  2. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  3. class template user defined obj
    By terracota in forum C++ Programming
    Replies: 4
    Last Post: 06-01-2004, 08:14 AM
  4. how do i create a class
    By chanuajohnson in forum C++ Programming
    Replies: 1
    Last Post: 09-14-2002, 01:36 PM
  5. Create class at run time
    By muaed in forum A Brief History of
    Replies: 3
    Last Post: 04-11-2002, 08:13 AM