accessing variable of a class without instantiating or using static keyword
I have a public/private variable (var) in class X, which I need to access from class Y.
The constraints are that class X is instantiated only once from the application. It cannot be instantiated in class Y again.
and
the variable (var) cannot be made static, as 2 different applications instantiating this class X should see the variable differently.
and cannot make the class Y nested in class X, also in class Y the func() arguments cannot be added, because this function is being called from somewhere in the state machine which passes on only 2 arguments.
class X
{
public:
int a;
};
class Y
{
static func(int aa, int bb)
{
a = 1; //want to perform the set/get operations from this class for 'a'...
}
};
require experts help in solving this,
thanks in advance.
Re: accessing variable of a class without instantiating or using static keyword
You should have a look at the Singleton Pattern.
Re: accessing variable of a class without instantiating or using static keyword
hi, class X cannot be singleton, as another application should be able to create one more instance of class X
i.e. app1 should create instance of class X
and app2 should also create its own instance of class X.
to be more clear... applications are accessing the One Network stack and class X is defined as part of network stack state machine.
Re: accessing variable of a class without instantiating or using static keyword
A Singleton is unique to a process.
You need pay special attention if you have a multithreaded envirnoment, see the section Singleton_pattern - Implementation
Re: accessing variable of a class without instantiating or using static keyword
Quote:
Originally Posted by
g97468
I don't know if it fits the bill but for simple Singleton needs I often use a so called Meyer's Singleton
Code:
class X
{
public:
int a;
};
//
X& ONE_X(); // X Singleton
//
class Y
{
static func(int aa, int bb)
{
ONE_X().a = 1; //want to perform the set/get operations from this class for 'a'...
}
};
// implementation on a .cpp file
#include X.h
X& ONE_X() {
static X x; // instantiated once
return x;
}
Re: accessing variable of a class without instantiating or using static keyword
So... you want two different programs, running different threads/process to access a same variable. Sounds to me like you need to store said variable outside of the process.
Depending on your needs, anything from text file, database, environment variable or even register could suit your needs.
Re: accessing variable of a class without instantiating or using static keyword
Quote:
hi, class X cannot be singleton, as another application should be able to create one more instance of class X
That statement makes no sense. A singleton is instantiated once in every application program instance. Also singleton provides access to the one and only instance via a static member function which returns the one and only instance to the user. So it guarantees one instance per application and provides easy access to the one and only object.