Choosing a class (Game development)

im curious if i can conditional create an instance of an object like this

1
2
3
4
5
6
7
if (something == something1)
{
Object object;

}

object.somethingelse();



then use the object functions

EDIT:
any suggestions about choosing a class then a way to create that object depending on what the person chose.

for example:
if the input is lets say "warrior"
then in the background the warrior object is initialized.
im trying to avoid initializing all possible classes in the background but only initialize what is picked
i do have warrior class derived from a character class
Last edited on
The object will only exist in the scope inside the if statement. Imagine if your statement was false. What then? You'd be trying to call a member function on a non-existent object.

EDIT: Why do you need to do this?
Last edited on
well since i cant do it that way.
im trying to create a game using c++

one of the things is picking a character and if the user put in input picking one of the classes, the game will create that class object.

i have thought of declaring all the possible classes but i think that would be unnecessary and have unnecessary memory allocated
You can use a factory pattern to create an inherited class based on an input:
http://login2win.blogspot.ca/2008/05/c-factory-pattern.html
thanks, that seems really useful
Topic archived. No new replies allowed.