Solved

How to define Global structure shared by 2 classes?

Posted on 2012-03-11
9
346 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
[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
  • 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
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!

 

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 250 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 40 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 100 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 34

Assisted Solution

by:sarabande
sarabande earned 110 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Prime numbers are natural numbers greater than 1 that have only two divisors (the number itself and 1). By “divisible” we mean dividend % divisor = 0 (% indicates MODULAR. It gives the reminder of a division operation). We’ll follow multiple approac…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

728 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