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!