1

In my project I have a class named globalClass which kepts global variables for each classes and their created instances.

The aim of this globalClass to send all other classes and their created instances the same value for its each variable. If one class's instance change that value, the others(including main.cpp) must see the new value.

Think that,

  1. I have a int variable named foo and defined as public and a value of 100 at globalClass.h;

class globalClass
{

public:
   int foo = 100;
}

  1. I have a another classes named class2nd,class3rd,class4th
  2. I have intances of class2nd,class3rd and class4th defined like (in main.cpp)
#include "class2nd.h"
#include "class3rd.h"
#include "class4th.h"

class2nd class2ndA;
class2nd class2ndB;
class2nd class2ndC;

class3rd class3rdA;
class3rd class3rdB;
class3rd class3rdC;

class4th class4thA;
class4th class4thB;
class4th class4thC;
  1. I can reach the foo of globalClass from any of the other classes like

(eg. class2nd)

#include "globalClass.h"

globalClass glb;

void globalClass::someFunction()
{
   glb.foo = 123;
}

  1. Now I have to see the foo as 123 from all other class but I see it as 100 as defined on globalClass.h. It can only be seen as 123 on class2nd.

What do I have to do to see the foo as unique from all classes and their instances.... ?

bladekel
  • 13
  • 3
  • You mean you have the definition `globalClass glb;` in multiple *.cpp files? That would violate the One Definition Rule. – aschepler Apr 26 '20 at 02:20

2 Answers2

1

One solution would be to use a Singleton for your global class. You can see for example this answer.

The problem here is that you are defining on class per compilation units (ie per .cpp file you compile) which means that would have effectively a different instance of your class in each of your cpp files.

You should also think about using the Singleton Design Pattern as it is rarely a good design to adopt when building an application. You can see this to understand more about its use cases!

Thomas Caissard
  • 846
  • 4
  • 10
  • But he's talking about different classes that not necessary have any relation between them. Singleton will allow to share information only between instances of the same class (or inherited classes). – SomeWittyUsername Apr 26 '20 at 07:56
0

As Thomas Caissard told, the solution is Singleton Pattern.

Here is how for anyone who is looking for solution rather than me...

Singleton.h

#ifndef Singleton_H
#define Singleton_H

class Singleton
{
    private:
    Singleton();
    ~Singleton();
    static bool isInit;


    public: 
    static Singleton* getInstance();
    int foo;
};

#endif

Singleton.cpp

#include "Singleton.h"

bool Singleton::isInit=false;

Singleton* Singleton::getInstance()
{
    static Singleton s;
    return isInit? &s:0;
}

Singleton::Singleton()
{   
    isInit=true;    
}

Singleton::~Singleton()
{   
    isInit=false;
}

main.cpp( or otherClass with same syntax )


#include "Singleton.h"

void main() {   
    Singleton* p = Singleton::getInstance();
    p->foo = 123; // Use variable from everywhere with same value....

    // Other staffs
}
bladekel
  • 13
  • 3
  • I don't see any point to `isInit`. It doesn't protect against Undefined Behavior if `Singleton::getInstance()` is called after `s` is destroyed. – aschepler Apr 26 '20 at 11:14