Solved

How to share a C++ class?

Posted on 2000-03-14
7
180 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
Technology Partners: 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

Technology Partners: 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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

734 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