Accessing a Variable

Started by
6 comments, last by ratha 13 years, 6 months ago
Hi!
I've declared a protected variable in a base class. I've assigned value and print the variable in a derived class. It prints correctly. I've printed the same variable inside another derived class. But it prints nothing. How to print the variable that is altered inside another derived class?
Advertisement
We'll need to see the code.
What language? Can we see some code?
C++. This is the base class.
class CGameState { protected:     s32 musicVolume,soundVolume;};


These two variables are altered inside a scrollbar changed event method of a derived class.
class CAudioMenuState : public CGameState{ public:    void GUIEvent(CGameManager* pManager,const SEvent& event)    {     if(event.GUIEvent.EventType == EGET_SCROLL_BAR_CHANGED)     {      s32 id = event.GUIEvent.Caller->getID();       switch(id)    {     case ID_MUSIC_SCROLLBAR:     {	musicVolume = m_pMusic->getPos();	printf("Music ScrollBar Value: %d \n",musicVolume);       break;    }   case ID_SOUND_SCROLLBAR:   {    soundVolume = m_pSound->getPos();    printf("Sound ScrollBar Value: %d \n",soundVolume);   }  } }    }};


These two variables has to be accessed from another class called COptionsMenuState. COptionsMenuState and CAudioMenuState are derived from the same class(CGameState where I've declared the variables).
I doesn't look like you posted the code that's actually causing the problem, but from what you describe, it sounds like maybe you're changing the values of the variables in one instance of the class, and expecting those changes to be reflected in other instances of the class.

If that is the case, be aware that each instance of the class has its own copy of musicVolume and soundVolume; changing the values of these variables in one instance will not affect the values of these variables in other instances.

If that's not the case, perhaps you could post the code that's causing the problem, and maybe provide some more details about the behavior you're seeing.

(Also, what do you mean by 'it prints nothing'? Do you mean it's printing a value but it's not the expected value? Or do you mean something else?)
Hi!
Again I'm going to describe the scenario. I've a scrollbar in a menu page. This menu page is written as a separate class(derived from the CGameState class). I've to get the current value of the scrollbar(slider). I got it with the following line code.

musicVolume = m_pMusic->getPos();

The above variable 'musicVolume' is declared in the base class(CGameState). I've to print this variable's(musicVolume) at another class which is derived from CGameState itself. I've tried declaring the variable 'musicVolume' as static also. It prints nothing(means it has to print what percentage I moved the slider). How to do this?
If you want to access one variable from multiple instances of a class or it's derived classes, you need to make the variable global or static.

However, it'd be better to not put the variable in your game state class at all - why should the music or sound volume be a per-state variable? Wouldn't it be better to have that in your sound or music manager, and have the per-state variable mean "desired" volume?
Thanks. Problem Solved.

This topic is closed to new replies.

Advertisement