Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to share a C++ class?

Posted on 2000-03-14
7
Medium Priority
?
184 Views
Last Modified: 2010-04-02
Hello,

I declared a class in a DLL, and I want to share its member variable when mutipl instances are loaded? How? (The varibles are initialed in the first instance)

Thanks
Zouying

If I didn't make myself clear, let me know.

0
Comment
Question by:zouying
  • 4
  • 2
7 Comments
 
LVL 1

Expert Comment

by:ntdragon
ID: 2618644
you didn't make yourself clear
0
 

Author Comment

by:zouying
ID: 2618672
class Test
{
int var = 1;
public:
void Function1();
void Function2();
};

void Test::Function1()
{
printf("This will run OK");
}

void Test::Function2()
{
var = 2;
printf("This will not run OK");
}


I put the above class in a DLL, and load this dll in different places. Function1 always run fine. But Function2 will not. Since ( I think), var is not shared.

So, what I ask is how to share the data which is inside a class. This is quite misunderstanding, since class is not the place to declare a variable, but the definition.

Zouying
0
 
LVL 1

Accepted Solution

by:
Deckmeister earned 400 total points
ID: 2618683
Declare var as a static class member.
So there will exist only one instance of var for all the class objects you create
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 1

Expert Comment

by:Deckmeister
ID: 2618690
Just a comment to explain my answer:

If you write
class Test
{
static int var;
public:
void Function1();
void Function2();
};

then if you declare the following:
Test obj1, obj2;

the 2 objects you've just created will use the same variable var.

0
 
LVL 1

Expert Comment

by:Deckmeister
ID: 2618705
In order to initialize your static member variable, simply write:
int  Test::var = 1;

at the same place you write the member functions definition.  
0
 

Author Comment

by:zouying
ID: 2618746
Is that the only way?

I have too many variables in the class.
0
 
LVL 1

Expert Comment

by:Deckmeister
ID: 2618772
Well, I don't really see another efficient solution.

Another way would be to write a value somewhere, for instance in the Windows Registry.
This value would tell how many instances of the class are running and would hold the values of your variables.
But of course, this isn't efficient at all if the value of the variables is changing a lot.
It may be a solution if they change only at initialization time and before freeing memory (at the end of your programm's execution).

Another solution???
You could use global variables, but this is a bad solution. Static variables are much better because their range is limited by the class.

Well, I think static members is the best solution. I cannot tell you a lot more, because I don't know the context of your work.
If I have a better idea, I tell you.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question