Solved

How to share a C++ class?

Posted on 2000-03-14
7
179 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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!

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

726 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