Solved

Attributes Sharing

Posted on 1998-12-29
34
172 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
Comment Utility
Edited text of question
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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
Comment Utility
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
 
LVL 3

Expert Comment

by:shaig
Comment Utility
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
Comment Utility
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
Comment Utility
I appriciate this answer, but I need to know more...
0
 

Author Comment

by:tangkh
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 3

Expert Comment

by:shaig
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
No thanks man. You are on your own.  Enjoy wastings people's time?
0
 
LVL 3

Expert Comment

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

Author Comment

by:tangkh
Comment Utility
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
Comment Utility
No to the point. does this answer help you?
0
 

Author Comment

by:tangkh
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now