473,834 Members | 1,800 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Two classes share common data - How to?

4 New Member
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 12779
uofm
1 New Member
How about this:

#include <vector>

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

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

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

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

Is that what you want?
Apr 11 '08 #2
viettrung
4 New Member
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 Recognized Expert Specialist
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 Recognized Expert Contributor
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
viettrung
4 New Member
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
viettrung
4 New Member
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 Recognized Expert Moderator Expert
class A
{
private:
std::vector< int > m_vCommon;

public:
A( void ){};
~A( void ){};
std::vector< int >* GetCommonVector Ptr( 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
2067
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 "friend to class". I am very disappointed because it is the way how C++ Compiler is designed. I assume that "friend to class" is not the good option. If CMain class is initialized before CA class, CB class, and CC class are initialized inside CMain...
2
1455
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 both named ERR_MyButton_Click, compiling loaded modules produced an error. I'm glad to see this is not the case in Access 97. Access 2.0 went so far as to disallow me from using the same line numbers in two different procedures of a form module....
3
2939
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 WebFormBase and UserControlBase. And then set all my webforms and usercontrols inherit from the appropriate one. But this forces me to replicate my code between to the two base classes.
3
1202
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 the data to disk as a dataset and was wondering if it was an accepted practice to load/add/edit/delete the data in the dataset from methods within the collections/classes or should I just use them as a container, managing all dataset transactions...
19
2238
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 iVal):m_iValue(iVal){}
0
922
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 includes an imported version of the XSD for the typed dataset. Each web service reference then has its own typed data set class . Even though the dataset is the same each web service reference refers to it via a different namespace. How do I get...
1
799
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
957
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 a regular expression - for instance, a bunch of postal codes. Obviously the commonality is the regular expression... they all matched. So how do I get the matching data itself from across these fields... T3A5N1 T3A5N2 T3A5N3 T3A5N4
6
2183
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 two derived classes. Second class has its own m_Base1 and m_Base2 and third class does the same. I am curious. How can second class and third class share the same m_Base1 and m_Base2? You define second class first and enter data into...
0
9800
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9651
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
7762
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6960
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5629
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5800
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4429
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3987
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3085
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.