470,848 Members | 1,210 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,848 developers. It's quick & easy.

Two classes share common data - How to?


I have two classes that share a common data list (specifically, a std::vector). This data list should be accessed by the two classes only, so I think using a global variable is not a good solution. Actually I am so confused to find an elegant way to store such common data for the two.

If you have any idea about that, please let me know.

Thanks alot!

Apr 11 '08 #1
7 11883
How about this:

#include <vector>

class A
std::vector< int > m_vCommon;

A( void ){};
~A( void ){};
std::vector< int >* GetCommonVectorPtr( void )
return &m_vCommon;

class B
std::vector< int >* m_pvCommon;

B( void ): m_pvCommon( NULL ){};
B( A& a )
m_pvCommon = a.GetCommonVectorPtr();
~B( void );

Is that what you want?
Apr 11 '08 #2
Hi uofm,

Thanks so much for your solution.

Actually I have thought about this solution before, but one point makes me not totally satisfied is that class A and B seem to be closely coupled to each other. Specifically class B should know about A to get the common data. Do you think so?

By the way, I have just googled for this matter and found out something called Monostate Pattern which, I think, may solve this matter better. Please see the following article for details: http://www.devx.com/getHelpOn/10Minu...16361/0/page/1

If you have interests in it, please give me your opinions about that. I would much appreciate.

Thanks again for your time.

Apr 11 '08 #3
3,652 Expert 2GB
If Class A and Class B are so closely related to each other, maybe they could be subclasses of the same superclass, Class C. Then Class C has the data both A and B need, and therefore each can access it.
Apr 11 '08 #4
463 Expert 256MB
The monostate design pattern only uses static variables for sharing information. This means that ALL class A & B objects share the same data and in your case, they would all share the same vector.

The solutions by uofm and ganon allow any two objects to share the same data. Here you would pass the list from one object to another.

Both are viable soultions, and you'll have to figure out which one works best for you.
Apr 11 '08 #5
Hi Ganon11,

Thanks alot for you suggestion. That is a good idea. But in my case, the two classes have no relationship else but sharing the common data list. So logically I do not want them to share the same parent.

Have a good day!

Apr 12 '08 #6
Hi RRick,

Your explaination is very clear for me to see the advantages of the three solutions in this matter. I will consider about them. Many thanks!

@all: Again, thank you very much for your support. I have learned many valuable experiences from you.

Best regards,

Apr 12 '08 #7
9,208 Expert Mod 8TB
class A
std::vector< int > m_vCommon;

A( void ){};
~A( void ){};
std::vector< int >* GetCommonVectorPtr( void )
return &m_vCommon;
Actually, this is not a good solution. Class A has broken encapsulation by returning a pointer to its private data member. That means the data member can be altered by functions that are not class A functions. That means there is no control of the contents of the database.

In effect this is a global variable parading as a member variable.

When you have common data, you need to ask whether the data can change independently from the class objects. If this is the case, the object should have a pointer (preferably a handle) to the data.

The data itself is managed by another object altogether.

Expand|Select|Wrap|Line Numbers
  1. class DataManager
  2. {
  3.     private:
  4.         vector<int> theData;
  5.     public:
  6.         SetTemperature(int arg);
  7.         int GetMaxTemperature();
  8.         //etc...
  9. };
Here the vector contains temperatures. The SetTemperature() can regulate what goes in the data be refusing input like 263634673 degrees. Merely exposing a pointer to the vector destroys this ability.

class B now has a pointer to a DataManager object. It is the DataManager that provides access to the data through its public interface.

You really want to avoid closely coupled classes since it is the path to monolithic code.
Apr 12 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Bryan Parkoff | last post: by
3 posts views Thread by Paul | last post: by
reply views Thread by balabaster | last post: by
6 posts views Thread by Immortal Nephi | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.