• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 190
  • Last Modified:

Attributes Sharing

I have 3 classes, in which one of this is a data storage class (says, classData), another two are the classes (says, classA and classB) that need to perform read and write data to this data class.

My question is, how can I share the data of classData between classA and classB, i.e. if I perform some functions within classA that write some data into classData, how can I extract the same data written by classA from within classB?

I know this can be done through the global variables, but I am trying to avoid the globals variables that may lead to some unauthorised access problems.

For example:

====== ClassData.h =======
Class classData
{
public:
    int data1;
    SOME_STRUCT *struct1;
}

====== ClassA.h =======
Class classA
{
public:
    void WriteToClassData();
}

====== ClassB.h =======
Class classB
{
public:
    void ReadFromClassData();
}
0
tangkh
Asked:
tangkh
  • 21
  • 9
  • 4
1 Solution
 
tangkhAuthor Commented:
Edited text of question
0
 
jkrCommented:
Well, as you already declared the members as public, i can't see any obstacle that keeps you from accessing the data ...
0
 
tangkhAuthor Commented:
The problem is, for example

==== File A ====
Class classData
{
public:
    int data1;
}

==== File B ====
SomeClass::function()
{
 ....
 classData myData;
 myData.data1 = 5;
}

===== File C =====
AnotherClass::Anotherfunction()
{
 classData myData;
  int a = myData.data1;     // Will I get a = 5???
}

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!

 
shaigCommented:
You have many options:
1) If myData is unik to the whole system, all of it's members can be static. (This way a=5 form anyware).  NOT RECOMENDED.
2) Additions to your declarations:

==== File B ====
SomeClass::function()
{
   SomeClass( classData &myData )   //constructor
   {
       m_myData = myData;
   }
 ....
 classData &m_myData;
  myData.data1 = 5;
}

===== File C =====
AnotherClass::Anotherfunction()
{
   AnotherClass( classData &myData )   //constructor
   {
       m_myData = myData;
   }

 classData &m_myData;
  int a = myData.data1;     // I Will get a = 5
}

main( ...)
{
  classData cdo;
  SomeClass sco;
  AnotherClass aco( cdo );
}

I also recomend to avoid the usage of public data members.
Use Set/Get functions, friends (for specific class functions), etc.
0
 
tangkhAuthor Commented:
Can you explain the following?

SomeClass::function()
{
       SomeClass( classData &myData )   //why do you call the constructor from within the
                                                            //fucntion of the class?
       {
           m_myData = myData;    <-------- What is this for?
       }
     ....
     classData &m_myData;
      myData.data1 = 5;
    }

Also, how to make use of the friend keyword in this case?
0
 
tangkhAuthor Commented:
I appriciate this answer, but I need to know more...
0
 
tangkhAuthor Commented:
maybe I should make it clear. For instance, I want to create a class (CSettings) to store all my communication settinngs. This settings are done through varies functions of a class (CConfigure). Then I have another class (CCommunicate) to perform the communication. How do you normally obtain the settings (stored in CSettings) done by the CConfigure from within the CCommunicate class?

p/s: These three classes are stored in three different .cpp and .h files.
0
 
tangkhAuthor Commented:
maybe I should make it clear. For instance, I want to create a class (CSettings) to store all my communication settinngs. This settings are done through varies functions of a class (CConfigure). Then I have another class (CCommunicate) to perform the communication. How do you normally obtain the settings (stored in CSettings) done by the CConfigure from within the CCommunicate class?

p/s: These three classes are stored in three different .cpp and .h files.
0
 
tangkhAuthor Commented:
maybe I should make it clear. For instance, I want to create a class (CSettings) to store all my communication settinngs. This settings are done through varies functions of a class (CConfigure). Then I have another class (CCommunicate) to perform the communication. How do you normally obtain the settings (stored in CSettings) done by the CConfigure from within the CCommunicate class?

p/s: These three classes are stored in three different .cpp and .h files.
0
 
tangkhAuthor Commented:
maybe I should make it clear. For instance, I want to create a class (CSettings) to store all my communication settinngs. This settings are done through varies functions of a class (CConfigure). Then I have another class (CCommunicate) to perform the communication. How do you normally obtain the settings (stored in CSettings) done by the CConfigure from within the CCommunicate class?

p/s: These three classes are stored in three different .cpp and .h files.
0
 
tangkhAuthor Commented:
maybe I should make it clear. For instance, I want to create a class (CSettings) to store all my communication settinngs. This settings are done through varies functions of a class (CConfigure). Then I have another class (CCommunicate) to perform the communication. How do you normally obtain the settings (stored in CSettings) done by the CConfigure from within the CCommunicate class?

p/s: These three classes are stored in three different .cpp and .h files.
0
 
jkrCommented:
Consider the following:
class CConfigure: public CWhatever
{
public:
 CConfigure();
 ~CConfigure();

long lSetting1;
long lSetting2;
long lSetting3;

/etc.
};

class CCommunicate: public CSomethingElse
{
 CCommunicate( CConfigure* pConf);
 ~CCommunicate();

 void Send(void);

private:
CComfigure* m_pConfiguration;
};

CCommunicate::CCommunicate( CConfigure* pConf)
{
 m_pConfiguration = pConf; // store ptr to object
}

void CCommunicate::Send()
{
 // use the pointer to the object to retrieve the value...
 long lSetting = m_pConfiguration->lSetting1;

//...
}

0
 
tangkhAuthor Commented:
maybe I should make it clear. For instance, I want to create a class (CSettings) to store all my communication settinngs. This settings are done through varies functions of a class (CConfigure). Then I have another class (CCommunicate) to perform the communication. How do you normally obtain the settings (stored in CSettings) done by the CConfigure from within the CCommunicate class?

p/s: These three classes are stored in three different .cpp and .h files.
0
 
jkrCommented:
OK, to elaborate:
Consider the following:
// configure.h
class CConfigure: public CWhatever
{
public:
 CConfigure();
 ~CConfigure();

long lSetting1;
long lSetting2;
long lSetting3;


//etc.
};

// configure.cpp
#include "configure.h"
// implementation goes here...

// communicate.h
#include "configure.h"
class CCommunicate: public CSomethingElse
{
 CCommunicate( CConfigure* pConf);
 ~CCommunicate();

 void Send(void);

private:
CComfigure* m_pConfiguration;
};

//communicate.cpp
#include "communicate.h"
CCommunicate::CCommunicate( CConfigure* pConf)
{
 m_pConfiguration = pConf; // store ptr to object
}

void CCommunicate::Send()
{
 // use the pointer to the object to retrieve the value...
 long lSetting = m_pConfiguration->lSetting1;

//...
}


0
 
tangkhAuthor Commented:
maybe I should make it clear. For instance, I want to create a class (CSettings) to store all my communication settinngs. This settings are done through varies functions of a class (CConfigure). Then I have another class (CCommunicate) to perform the communication. How do you normally obtain the settings (stored in CSettings) done by the CConfigure from within the CCommunicate class?

p/s: These three classes are stored in three different .cpp and .h files.
0
 
tangkhAuthor Commented:
maybe I should make it clear. For instance, I want to create a class (CSettings) to store all my communication settinngs. This settings are done through varies functions of a class (CConfigure). Then I have another class (CCommunicate) to perform the communication. How do you normally obtain the settings (stored in CSettings) done by the CConfigure from within the CCommunicate class?

p/s: These three classes are stored in three different .cpp and .h files.
0
 
tangkhAuthor Commented:
Hi jkr,

Here is what I want.... referring to your example, supposely I have other class(es) wanting to access the attributes of class CConfigure, in your example lSetting1etc, to obtain the value set by the functions within CConfigure and CCommunicate, how can I do that?

0
 
shaigCommented:
Sorry, my answer was correct.
What you asked about was a simple refference passing.
The class has a refference member variable to a configuration object.  In the constructor, this member is initialized. If you don't know what references are all about, you should use pointers in a very simular way.
0
 
tangkhAuthor Commented:
Hi shaig,

I think your answer is closer to what I want, but I do not understand the following:

SomeClass::function()
{
   SomeClass( classData &myData )   //constructor  <--- How can you build the constructor within a function???
   {
       m_myData = myData;    <------ what is m_myData??
   }
   ....
   classData &m_myData;
   myData.data1 = 5;
}
0
 
shaigCommented:
It is a refference variable to a classDATA object.
I made a mistake with the copy/paste, it should look like this:

class SomeClass
{
   SomeClass( classData &myData ) 
   {
       m_myData = myData;    
       m_myData.data1 = 5;
   }
   ....
   classData &m_myData;   <------ this is m_myData declaration
}

When m_myData is changed, the actual data is changed rather then a local copy of it. i belive that this is what you wanted.
0
 
tangkhAuthor Commented:
Hi shaig,

I think your answer is closer to what I want, but I do not understand the following:

SomeClass::function()
{
   SomeClass( classData &myData )   //constructor  <--- How can you build the constructor within a function???
   {
       m_myData = myData;    <------ what is m_myData??
   }
   ....
   classData &m_myData;
   myData.data1 = 5;
}
0
 
shaigCommented:
Give a closer view on my last answer. It is self explanitory.
I you are not familiare with & (references) in c++, then please let me know about it and i'll give you a poinetr based answer.
0
 
tangkhAuthor Commented:
Hi shaig,

I think your answer is closer to what I want, but I do not understand the following:

SomeClass::function()
{
   SomeClass( classData &myData )   //constructor  <--- How can you build the constructor within a function???
   {
       m_myData = myData;    <------ what is m_myData??
   }
   ....
   classData &m_myData;
   myData.data1 = 5;
}
0
 
shaigCommented:
No thanks man. You are on your own.  Enjoy wastings people's time?
0
 
shaigCommented:
No thanks man. You are on your own.  Enjoy wastings people's time?
0
 
tangkhAuthor Commented:
I agree it is annoying, but it was not me who did that. I sent the message only once. I do not have such time to do so.
0
 
shaigCommented:
No to the point. does this answer help you?
0
 
tangkhAuthor Commented:
I agree it is annoying, but it was not me who did that. I sent the message only once. I do not have such time to do so.
0
 
tangkhAuthor Commented:
Hi shaig,

Ok, please post your comment as answer, and final question: dont I need to declare the variable m_mydata in

m_myData = myData;  

?
0
 
shaigCommented:
No since it is one of the classes data members.
I can't post it as an answer because you haven't rejected the proposed one. once you do i will post it again.
0
 
jkrCommented:
Reject my answer, i'm out of this - way too annoying... (how often should i have explained it again and again and again???)
0
 
tangkhAuthor Commented:
Hi shaig,

Ok, please post your comment as answer, and final question: dont I need to declare the variable m_mydata in

m_myData = myData;  

?
0
 
tangkhAuthor Commented:
Hi shaig,

Ok, please post your comment as answer, and final question: dont I need to declare the variable m_mydata in

m_myData = myData;  

?
0
 
shaigCommented:

I can't post it as an answer because you haven't rejected the proposed one. once you do that, i will post it again.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 21
  • 9
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now