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

  1. #1
    Registered User
    Join Date
    Jul 2012

    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:
    #ifndef CROOM_H
            #define    CROOM_H
    #include <SDL/SDL.h>
    //=Constant Room IDs declaration (add new rooms here)===========================
    //=Class Declaration============================================================
    class CRoom {
        static CRoom RoomControl;
        RoomID_Type ActiveRoomID;
        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:
    #include "CRoom.h"
    //=Static Object Declaration====================================================
    CRoom CRoom::RoomControl;
    //=CRoom(): Constructor=========================================================
    CRoom::CRoom() {
        ActiveRoomID = BOOT_UP; 
    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
    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
    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
    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, 11:01 AM
  2. Replies: 8
    Last Post: 02-14-2010, 04: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, 06:48 AM
  5. a noobish question on variable scope
    By deviousdexter in forum C# Programming
    Replies: 14
    Last Post: 12-03-2008, 11:53 PM