|
-
September 20th, 2010, 04:43 AM
#1
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.
-
September 20th, 2010, 04:46 AM
#2
Re: accessing variable of a class without instantiating or using static keyword
You should have a look at the Singleton Pattern.
your humble savant
-
September 20th, 2010, 06:24 AM
#3
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.
-
September 20th, 2010, 06:28 AM
#4
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
your humble savant
-
September 20th, 2010, 06:51 AM
#5
Re: accessing variable of a class without instantiating or using static keyword
 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;
}
Last edited by nuzzle; September 20th, 2010 at 06:59 AM.
-
September 20th, 2010, 07:00 AM
#6
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.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
September 22nd, 2010, 01:07 PM
#7
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
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|