Use Singleton pattern, something like this:
singleton.h
#ifndef SINGLETON_H
#define SINGLETON_H
class singleton
{
private:
singleton();
int p_value;
public:
static singleton * instance();
int GetValue();
void SetValue(int value);
};
#endif // SINGLETON_H
#ifndef SINGLETON_H
#define SINGLETON_H
class singleton
{
private:
singleton();
int p_value;
public:
static singleton * instance();
int GetValue();
void SetValue(int value);
};
#endif // SINGLETON_H
To copy to clipboard, switch view to plain text mode
singleton.cpp
#include "singleton.h"
singleton * p_instance = 0;
singleton::singleton()
{
}
singleton * singleton::instance()
{
if(!p_instance) p_instance = new singleton();
return p_instance;
}
int singleton::GetValue()
{
return p_value;
}
void singleton::SetValue(int value)
{
p_value = value;
}
#include "singleton.h"
singleton * p_instance = 0;
singleton::singleton()
{
}
singleton * singleton::instance()
{
if(!p_instance) p_instance = new singleton();
return p_instance;
}
int singleton::GetValue()
{
return p_value;
}
void singleton::SetValue(int value)
{
p_value = value;
}
To copy to clipboard, switch view to plain text mode
so in each file you need this object just include
#include "singleton.h"
//and use it in one place
singleton::instance()->SetValue(1);
// in other place
int value = singleton::instance()->GetValue();
#include "singleton.h"
//and use it in one place
singleton::instance()->SetValue(1);
// in other place
int value = singleton::instance()->GetValue();
To copy to clipboard, switch view to plain text mode
Bookmarks