Solved

How to share a C++ class?

Posted on 2000-03-14
7
176 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 100 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now