Sometimes things work, sometimes compiler won't compile usually gives "blahblah a non static member...error" or "blahblah cannot resolve external symbol...error".
Sometimes you just need a variable to be in the global scope...
..using the extern keyword:
http://www.cplusplus.com/forum/general/21368/
//FILE: includes.h
//extern tells the compiler that the variable is defined
//somewhere else, so it doesn't complain about it being undefined.
extern int count;
//FILE: main.cpp
#include "includes.h"
int count = 4;
//FILE other.cpp
#include "includes.h"
std::cout << count << std::endl;// will output 4
other stuff:
http://www.cplusplus.com/doc/tutorial/classes/
http://www.learncpp.com/cpp-tutorial/82-classes-and-class-members/
http://www.learncpp.com/cpp-tutorial/813-friend-functions-and-classes/
//C++ How to control variables between classes
//first define your prototype variable and functions (static)
//in a header file:
//example.h
class ScrollingBackground
{
public:
static bool m_bScroll;
static void scrollOn();
static void scrollOff();
}
//Then in the example.cpp file declare a variable to "activate" it
//preferebly in the top section
//example.cpp
bool ScrollingBackground::m_bScroll = true;
void scrollOn()
{
ScrollingBackground::m_bScroll = true;
}
void scrollOff()
{
ScrollingBackground::m_bScroll = false;
}
//Then use #include "example.h" in any file you'd
//like to use the functions scrollOn and scrollOff
//and you can control the variable m_bScroll
Another example to "share" variables between classes use the "friend" syntax:class Accumulator
{
private:
int m_nValue;
public:
Accumulator() { m_nValue = 0; }
void Add(int nValue) { m_nValue += nValue; }
//Make the Reset() function a friend of this class
friend void Reset(Accumulator &cAccumulator);
}
//Reset() is now a friend of the Accumulator class
void Reset(Accumulator &cAccumulator)
{
//And can access the private data of Accumulator objects
cAccumulator.m_nValue = 0;
}
Below is another example of how to use static variables in multiple files and classes,
in other words: read/write/update variable between files and classes. "Global" access.
////////////////////////////////////////////////////////////////////
//define prototype variables and functions in the public section of your class Player.h
public:
static int m_iXPlayer;
static int m_iYPlayer;
//their values can be accessed in other files if #include "Player.h" is used
//IMPORTANT! need to be declared at the top in player.cpp file TO BE ACTIVATED
//and important to use syntax: variableType className::variableName = value;
//like below. If not the compiler will give you error: cannot resolve external symbol (blabla@..)
int Player::m_iXPlayer = 0;
int Player::m_iYPlayer = 0;
//they can then be used/updated in Player.cpp
//and then you can read their value in other files and classes
//one way to access their values in other classes is using them in functions and return their value
//in another file named example.h
#include "player.h"
class example
{
public:
int iGetX(){return Player::m_iXPlayer;}
int iGetY(){return Player::m_iYPlayer;}
}
//then use iGetX() and iGetY() in your code to get the values of the variables
No comments:
Post a Comment
Please, no foul language, trolling, keep it clean. Thanx.