Solved

Attributes Sharing

Posted on 1998-12-29
34
186 Views
Last Modified: 2013-11-20
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
Comment
Question by:tangkh
[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
  • 21
  • 9
  • 4
34 Comments
 

Author Comment

by:tangkh
ID: 1326848
Edited text of question
0
 
LVL 86

Expert Comment

by:jkr
ID: 1326849
Well, as you already declared the members as public, i can't see any obstacle that keeps you from accessing the data ...
0
 

Author Comment

by:tangkh
ID: 1326850
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
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 3

Expert Comment

by:shaig
ID: 1326851
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
 

Author Comment

by:tangkh
ID: 1326852
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
 

Author Comment

by:tangkh
ID: 1326853
I appriciate this answer, but I need to know more...
0
 

Author Comment

by:tangkh
ID: 1326854
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
 

Author Comment

by:tangkh
ID: 1326855
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
 

Author Comment

by:tangkh
ID: 1326856
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
 

Author Comment

by:tangkh
ID: 1326857
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
 

Author Comment

by:tangkh
ID: 1326858
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
 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
ID: 1326859
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
 

Author Comment

by:tangkh
ID: 1326860
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
 
LVL 86

Expert Comment

by:jkr
ID: 1326861
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
 

Author Comment

by:tangkh
ID: 1326862
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
 

Author Comment

by:tangkh
ID: 1326863
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
 

Author Comment

by:tangkh
ID: 1326864
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
 
LVL 3

Expert Comment

by:shaig
ID: 1326865
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
 

Author Comment

by:tangkh
ID: 1326866
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
 
LVL 3

Expert Comment

by:shaig
ID: 1326867
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
 

Author Comment

by:tangkh
ID: 1326868
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
 
LVL 3

Expert Comment

by:shaig
ID: 1326869
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
 

Author Comment

by:tangkh
ID: 1326870
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
 
LVL 3

Expert Comment

by:shaig
ID: 1326871
No thanks man. You are on your own.  Enjoy wastings people's time?
0
 
LVL 3

Expert Comment

by:shaig
ID: 1326872
No thanks man. You are on your own.  Enjoy wastings people's time?
0
 

Author Comment

by:tangkh
ID: 1326873
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
 
LVL 3

Expert Comment

by:shaig
ID: 1326874
No to the point. does this answer help you?
0
 

Author Comment

by:tangkh
ID: 1326875
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
 

Author Comment

by:tangkh
ID: 1326876
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
 
LVL 3

Expert Comment

by:shaig
ID: 1326877
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
 
LVL 86

Expert Comment

by:jkr
ID: 1326878
Reject my answer, i'm out of this - way too annoying... (how often should i have explained it again and again and again???)
0
 

Author Comment

by:tangkh
ID: 1326879
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
 

Author Comment

by:tangkh
ID: 1326880
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
 
LVL 3

Expert Comment

by:shaig
ID: 1326881

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

Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
In this post we will learn different types of Android Layout and some basics of an Android App.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

626 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