Question regarding the scope of a class variable in specific situation.

This is a discussion on Question regarding the scope of a class variable in specific situation. within the C++ Programming forums, part of the General Programming Boards category; Hi all, I am currently writing a quite simple program in C++ with the SDL library. I am not blocked ...

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

    Question regarding the scope of a class variable in specific situation.

    Hi all,

    I am currently writing a quite simple program in C++ with the SDL library. I am not blocked at any point right now but I have hard times understanding the scope of a class member variable, because it seems to be not always the same, and I was wondering why.

    So I got this code in my header file, which declares the CRoom class:
    Code:
    #ifndef CROOM_H
            #define    CROOM_H
    
    #include <SDL/SDL.h>
    
    //=Constant Room IDs declaration (add new rooms here)===========================
    enum RoomID_Type { NONE, BOOT_UP, INTRO, MAIN_MENU, FILE_SELECT, LEVEL_SELECT, IN_GAME, CREDITS};
    
    //=Class Declaration============================================================
    
    class CRoom {
       
    public:
        
        static CRoom RoomControl;
        
    private:
    
        RoomID_Type ActiveRoomID;
        
    public:
        
        CRoom();
        
        static RoomID_Type GetActiveRoomID();
        static void SetActiveRoomID(RoomID_Type RoomID); 
    };
    //==============================================================================
    #endif    /* CROOM_H */
    Note that I defined the RoomID_Type above my class, and the variable we are interested in is the ActiveRoomID.

    Now here is my source file's code for this class:
    Code:
    #include "CRoom.h"
    
    //=Static Object Declaration====================================================
    CRoom CRoom::RoomControl;
    
    //=CRoom(): Constructor=========================================================
    CRoom::CRoom() {
        
        ActiveRoomID = BOOT_UP; 
    }
    
    //=GetActiveRoomID()============================================================
    RoomID_Type CRoom::GetActiveRoomID() {
        
        return CRoom::ActiveRoomID;
    }
    My problem is in the last function, "GetActiveRoomID()". Notice that in order to return the ActiveRoomID variable, I need to access it this way: CRoom::ActiveRoomID. However, I don't understand why since this variable and this function are parts of the same class. In other classes with similar situations, I can simply return the variable without accessing it with the scope operator ( :: ).

    I was wondering if it was because of the Enum being declared outside the class, so I tried declaring it inside the class, but it doesn't change anything.

    Anyone knows why the ActiveRoomID's scope is out of reach for a member function of the same class? Thanks for taking the time to read this!
    Last edited by MajorScientist; 07-25-2012 at 02:08 PM.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I don't see how your code can compile in either configuration.

    CRoom::GetActiveRoomID() is a static function accessing a non-static member variable.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    6
    Thanks to your short answer, I just figured it out. My function shouldn't be static anymore because I changed some design, I just didn't even realize it was still declared as a static function. Too much hours working on a code sometimes lead me to miss such simple details.

    Thanks again.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    2,370
    to get/set ActiveRoomID on the static member, you need to use CRoom::RoomControl.ActiveRoomID.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class Scope & Locking question
    By Toonzaka in forum C++ Programming
    Replies: 2
    Last Post: 01-20-2011, 10:01 AM
  2. Replies: 8
    Last Post: 02-14-2010, 03:14 PM
  3. Replies: 6
    Last Post: 08-12-2009, 04:46 AM
  4. variable scope
    By happyclown in forum C Programming
    Replies: 4
    Last Post: 03-02-2009, 05:48 AM
  5. a noobish question on variable scope
    By deviousdexter in forum C# Programming
    Replies: 14
    Last Post: 12-03-2008, 10:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21