Choosing a class (Game development)

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

if (something == something1)
Object object;



then use the object functions

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:
thanks, that seems really useful
Topic archived. No new replies allowed.