Assuming that this is syntax an oriented question:
class Data_Class {
public:
static int a;
static int GetA() const { return a; }
}
In ClassA
:
Data_Class::a;
Data_Class::GetA();
Don't forget to add Data_Class
static members definitions, for example into dataclass.cpp
:
static int Data_Class::a = 0;
Otherwise you'll get an undefined reference
linker error.
You also may consider using singleton pattern (according to comments, singleton is bad idea and you should avoid it if possible) as Aubin suggested which will take care of adding new static member definitions:
class Data_Class {
private:
Data_Class(); // Prevents multiple instances
int a;
int b;
public:
// Will initialize instance if needed
static Data_Class &GetInstance(){
static Data_Class instance;
return instance;
}
int GetA() const {return a;}
};
Usage:
Data_Class::GetInstance().GetA()
Or go with little more complex but more flexible Dependency injection which is according to wiki:
Dependency injection is a software design pattern that allows a choice
of component to be made at run-time rather than compile time. This can
be used, for example, as a simple way to load plugins dynamically or
to choose mock objects in test environments vs. real objects in
production environments. This software design pattern injects the
dependent element (object or value etc) to the destination
automatically by knowing the requirement of the destination.