?
Solved

How to define Global structure shared by 2 classes?

Posted on 2012-03-11
9
Medium Priority
?
352 Views
Last Modified: 2012-03-17
Want to have a global data structure which can be shared by several classes or shared by several files. If the data structure in one of classes is updated, the other classes can use the same updated data in their own classes.  A workable code snippet is highly appreciated.
0
Comment
Question by:jl66
  • 3
  • 2
  • 2
  • +2
9 Comments
 

Author Comment

by:jl66
ID: 37707078
Any gurus: Is it possible?
0
 
LVL 86

Expert Comment

by:jkr
ID: 37707217
What you are describing pretty much sounds like the Singleton Design Pattern. Read more about it at http://sourcemaking.com/design_patterns/singleton and check the code examples at http://sourcemaking.com/design_patterns/singleton/cpp/2
0
 
LVL 29

Expert Comment

by:pepr
ID: 37707711
Actually, any global variable can be shared by whatever part of code.  You should better specify your goal.
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!

 

Author Comment

by:jl66
ID: 37707889
Thanks a lot for the tips.

How to define the global variables (structure) that can be shared by two classes (4 files: assume 2 per class: .h and .cpp) before exploring the design pattern? For example,

1) have structure Area
struct Area {
  char Name[10];
  unsigned int x;
  unsigned int y;
};

2) One class
class Expense
{
   // It may assign/update some values into Area and do some thing
};

3) another class
class Pollution
{
  // It will calculate the pollution value with the area.
};

How to make the struct Area as global structure used by these 2 classes?
0
 
LVL 86

Accepted Solution

by:
jkr earned 1000 total points
ID: 37707908
Well, the simply make it a global variable, e.g.

#ifndef AREA_H // header guard
#define AREA_H
// area.h
struct Area {
  char Name[10];
  unsigned int x;
  unsigned int y;
};
#ifndef MAIN_CPP
extern Area theGlobalArea;
#endif
#endif

// expense.h
#include "Area.h"
// other declarations

// pollution.h
#include "Area.h"
// other declarations

// main.cpp
#define MAIN_CPP
#include "pollution.h"
#include "expense.h"

Area theGlobalArea; // global instance

int main () {

  // do stuff

  return 0;
}

Open in new window

0
 
LVL 29

Assisted Solution

by:pepr
pepr earned 160 total points
ID: 37708637
... but you should be aware of the fact that global variables can be a source of very nasty errors if the code becomes a bit more complex.  The reason is that the change in one global variable affects all the code that uses the variable.  Sometimes (and especially in GUI applications or in other applications with more logical threads of execution), the modifications of the global variables may happen when you do not expect it.  Sometimes, it may be difficult to demonstrate the problem later (i.e. difficult to debug).  So, there is a big warning when using global variables.  It may be OK if the global variable is filled by few parts of code and then mostly read.

Anyway, it may not fit very well with Object Oriented Design that is basically based on "cooperation of objects".  If the "external" data should only be observed by many places in your code, then the Observer pattern may be the right one (http://en.wikipedia.org/wiki/Observer_pattern).  Basically, you ask the observed object to tell you when something changes.
0
 
LVL 3

Assisted Solution

by:kalpeshchavan
kalpeshchavan earned 400 total points
ID: 37708743
use Singleton pattern

ex
class ABC {

private :
   ABC(){};
public :
int data;
static ABC &getInstance()
{
   static ABC _ins;
   return &_ins;
}

};

// to get Instance of object

ABC &_a=ABC::getInstance();

cout<<_a.data<<endl ;

Open in new window


note : in singleton patter constructor is private and getInstance method is private and it return referance of object ()
0
 
LVL 35

Assisted Solution

by:sarabande
sarabande earned 440 total points
ID: 37709492
the normal way to share data between classes is to have a pointer or reference to the same object or to have pointers or references between the classes themselves:

struct Data
{
    whatevertype wet;
};


class B
{
      // a reference to a Data object
      Data & data;
public:
      // a constructor that takes a Data reference and initializes data member with it
      B(Data & d) : data(d) {  data.wet = ...; }    
};

class A
{
     // contains normal data member
     Data data;
public:
    void memberfunc()
    {
           // create B object   and pass data member
           B b(data);

           // now the data member was shared between class A and B 
    }     
};

Open in new window



struct Data
{
    whatevertype wet;
};


// forward declaration:
class A;

class B
{
      A* parent; 
public:
      // a constructor that takes a reference to A
      B(A* pa) : parent(pa) {  }    

      void memberfunction();
};

class A
{
     // contains normal data member
     Data data;
public:
    Data & getData()
    {
           // create B object   and pass 'this' pointer as parent
           B b(this);
           b.memberfunction();
         
    }     
};

void B::memberfunction()
{
      // get data from parent
      Data & data = parent->getData();
      
      // now share data object with 'parent' of class A
}

Open in new window


Sara
0
 

Author Closing Comment

by:jl66
ID: 37733984
Thanks a lot for everyone's tips. Very helpful.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

Okay. So what exactly is the problem here? How often have we come across situations where we need to know if two strings are 'similar' but not necessarily the same? I have, plenty of times. Until recently, I thought any functionality like that wo…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

830 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