Thread: Scope problems when creating new objects

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    6

    Scope problems when creating new objects

    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:
    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;
        }
    
    }
    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.

    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!

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by MajorScientist View Post
    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.
    That's not how dynamic memory allocation works. Memory allocated dynamically stays put until either the program cleans it up or the program ends and the OS cleans it up (you should be in the habit of cleaning it up yourself prior to the program ending).

    Are these CBootUp objects unique to a particular instance of the CRoom class? Does each CRoom object have a single/multiple(variable #) of these CBootUp objects? If so, then it would seem that the CRoom class needs either a CBootUp data member or perhaps a pointer (smart pointer) to one.

    If you need to maintain a variable number of CBootUp objects independent of the CRoom class objects then I might suggest perhaps a std::vector of CBootUp objects (or of pointer/smart-pointer to CBootUp objects). This vector could be passed into the RoomLoad function by reference and relieve you of the necessity for this container to be global. The RoomLoad function would add (push_back) CBootUp objects into the vector.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    6
    Quote Originally Posted by hk_mp5kpdw View Post
    That's not how dynamic memory allocation works. Memory allocated dynamically stays put until either the program cleans it up or the program ends and the OS cleans it up (you should be in the habit of cleaning it up yourself prior to the program ending).

    Are these CBootUp objects unique to a particular instance of the CRoom class? Does each CRoom object have a single/multiple(variable #) of these CBootUp objects? If so, then it would seem that the CRoom class needs either a CBootUp data member or perhaps a pointer (smart pointer) to one.

    If you need to maintain a variable number of CBootUp objects independent of the CRoom class objects then I might suggest perhaps a std::vector of CBootUp objects (or of pointer/smart-pointer to CBootUp objects). This vector could be passed into the RoomLoad function by reference and relieve you of the necessity for this container to be global. The RoomLoad function would add (push_back) CBootUp objects into the vector.
    In fact, the CRoom object is static, it's a singleton. Its main role is to create, loop, render and clean rooms. So when it creates a CBootUp object, there will never be more than one, and it will be deleted just before creating an object for another room, for example, CMainMenu object.

    If I understand you correctly, when I create an object inside a function, it won't be deleted when the function ends like a simple variable? And if I have a pointer to this object, it stays in memory until I delete it myself?

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Objects allocated dynamically live until deleted (either by the user or the OS). A pointer local to a function or sub-block of code within a function will be pushed/popped off the stack as per the usual rules for such items but if the pointer points to an object that was dynamically allocated, the allocated object remains even though the pointer itself that references the object may disappear once the pointer goes out of scope. So, one needs to make sure they don't lose that handle to the dynamically allocated object or they won't any way to reference it later or properly free it.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Jul 2012
    Posts
    6
    Quote Originally Posted by hk_mp5kpdw View Post
    Objects allocated dynamically live until deleted (either by the user or the OS). A pointer local to a function or sub-block of code within a function will be pushed/popped off the stack as per the usual rules for such items but if the pointer points to an object that was dynamically allocated, the allocated object remains even though the pointer itself that references the object may disappear once the pointer goes out of scope. So, one needs to make sure they don't lose that handle to the dynamically allocated object or they won't any way to reference it later or properly free it.
    It is extremely clear to me now, thanks for your answer. Do I absolutely need to use the "New" keyword to declare my CBootUp object, or declaring it as written in my original post works well?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, you need to use new to dynamically allocate an object.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jul 2012
    Posts
    6
    Quote Originally Posted by Elysia View Post
    No, you need to use new to dynamically allocate an object.
    Many thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File-scope objects
    By BdON003 in forum C++ Programming
    Replies: 11
    Last Post: 11-23-2009, 04:16 PM
  2. Creating Objects
    By vb.bajpai in forum C++ Programming
    Replies: 8
    Last Post: 06-20-2007, 09:53 PM
  3. creating objects in c++
    By sachitha in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2004, 12:19 PM
  4. creating objects
    By codec in forum C++ Programming
    Replies: 5
    Last Post: 10-09-2003, 04:25 PM