Hi all,
I have a question regarding scope when creating new objects. I have a static object which performs important global operations in my program. This static object has a method to check a variable, and depending of its value, creates an object of given type, which is different for different values. Here is an example:
However, if I create my CBootUp object inside that function, I won't be able to use it outside that function, and correct me if I'm wrong, but I believe it will even be deleted when the function ends.Code:void CRoom::RoomLoad() { switch (ActiveRoomID) { case BOOT_UP: CBootUp BootUpObject; BootUpObject.OnLoad(); break; case INTRO: //Insert function; break; case MAIN_MENU: //Insert function; break; } }
So I would like to create an object with this function, but would need it to be in a "global" scope. I don't think that creating a static object would be a good idea since this object is meant to be deleted and replaced with another one depending on where I go in my program. And if I do so, all those "Room" objects will need to be static, thus being created from the very beginning of the program and wasting resources when they aren't needed.
I also thought about using pointers, but again, if I create an object inside the function, assign its address to a "global" pointer, then the object will still be deleted when the function ends and the pointer will now point to nothing.
What would be the best solution for this? Thanks!