473,218 Members | 1,918 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,218 software developers and data experts.

Two classes share common data - How to?

Hi,

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!

.viettrung.
Apr 11 '08 #1
7 12644
uofm
1
How about this:

#include <vector>

class A
{
private:
std::vector< int > m_vCommon;

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

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

public:
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.

.viettrung.
Apr 11 '08 #3
Ganon11
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
RRick
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!

.viettrung.
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,

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

public:
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. };
  10.  
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

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

Similar topics

3
by: Bryan Parkoff | last post by:
I have C++ Primer Third Edition -- Author Stanley B. Lippman and Josee Lajoie. I have been studying it for couple months however it does not provide a valuable information which it is about...
2
by: MLH | last post by:
A97 allows separate procedures in a form module to share common label names. Access 2.0 would puke when that happened. For instance, if I had 2 procedures in an Access 2.0 form module with labels...
3
by: Garth17 | last post by:
I'm trying to figure out a solution for sharing common properties and methods in all me .aspx and .ascx pages. In classic ASP I would use include directives. So far I have made 2 base classes...
3
by: Paul | last post by:
Hello, I'm upgrading a small single user app to VB.NET and have a few questions about loading classes & collections classes with the data from a one to many dataset structure. I'm serializing...
19
by: scroopy | last post by:
Is it impossible in C++ to create an assignment operator for classes with const data? I want to do something like this class MyClass { const int m_iValue; public: MyClass(int...
0
by: David Worthington | last post by:
Hi, I have a solution with multiple web services that share the same typed dataset. Each time I add a reference to a web service to a client application it creates a new web refrence class that...
1
by: Nayana Thara | last post by:
We have a web site where we have asp and asp.net. how can we share session data b/w the two environments.
0
balabaster
by: balabaster | last post by:
How would I write a query to give me back the common piece of data that exists across multiple fields... I'll give a basic example...I've got a bunch of fields that were returned as the result of...
6
by: Immortal Nephi | last post by:
First class is the base class. It has two data: m_Base1 and m_Base2. Second class and third class are derived classes and they are derived from first class. m_Base1 and m_Base2 are inherited into...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.