Solved

How to share a C++ class?

Posted on 2000-03-14
7
177 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

910 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

20 Experts available now in Live!

Get 1:1 Help Now