Solved

Attributes Sharing

Posted on 1998-12-29
34
179 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
  • 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need to retreive data from .TFS file exported by DVR 8 195
ffmpeg - "rtsp://...... Operation not permitted" 4 80
wordmultiple challenge 12 131
Work with App store 7 51
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
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.

786 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