0

Let's say I have this code (minimal example):

class ImportantClass(){
   public:
      ImportantStuff(){};
      ~ImportantStuff(){};

      someVector       importantStuff1;
      someStruct       importantStuff2;
      someType         importantStuff3;
      someClass        importantStuff4;

      void fillImportantStuff(){
         //...do stuff
       }             
};

class ClassA{
   public: 
      ClassA(){};
      ~ClassA(){};

      //...functions that need access to "importantStuff1,2,3,4"
};

int main(){
   ImportantClass importantObject;
   importantObject.fillImportantStuff();

   ClassA object1;
   ClassA object2;
   ClassA object3;
   ClassA object4;

return 0;
}

And in words:

I have one class (ImportantClass) of which I will probably just have one object holding some different variables. I then have another class (ClassA) of which I will have many objects. Each of these object should have access to the variables in the object "importantObject".

I tried this:

class ClassA{
   private:
      ImportantClass* _importantClassObject;
   public: 
      ClassA(ImportantClass* x){_importantClassObject = x;};
      ~ClassA(){};

      //...functions that need access to "_importantClassObject"
};

But when I create an object of ClassA:

ImportantClass importantObject;
importantObject.fillImportantStuff();

ClassA object1(&importantObject);

I get a argument list mismatch error.

I guess my question would come down to:

What is common practice to share an object holding a lot of data with multiple objects of a different class?

remi
  • 937
  • 3
  • 18
  • 45
  • 1
    Passing a pointer to constructor should work. Show the actual code, and the full text of the error. That said, see also: https://en.wikipedia.org/wiki/Singleton_pattern – Igor Tandetnik Oct 23 '16 at 22:01
  • 1
    Please refer to the help center for how to write a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) – StoryTeller - Unslander Monica Oct 23 '16 at 22:01
  • Reading the comments, I understand that this should actually work. So I see now I'm not able to replicate the exact problem in minimal example. Sorry for a "wrong post". I will check again my real problem, and come back to this. Should I delete this? – remi Oct 24 '16 at 07:03

1 Answers1

1

The pointer should work, but the problem with it is that you end up not knowing who is responsible of cleaning up that shared object. And further, you need to modify a lot of consutrctors and classes when your shared class is even used in more than ClassA.

When data is shared across multiple (likely also different) classes, the Singleton pattern is useful. A very good example is the accepted answer here: C++ Singleton design pattern

The idea is that you have an class with a static accessor method (often something similar to SingletonClass::GetInstance();) to the single instance of that class in your program and hide the constructor for outside access. Then, you can use that method from everywhere to access exactly the same class intance (because there is globablly only one) with same members and methods of that class. :)

Community
  • 1
  • 1
Peter Merkert
  • 354
  • 5
  • 14
  • Or you could just use a global variable. – melpomene Oct 23 '16 at 22:12
  • The problem with that is, that you have no responsibility for initialization. This could be covered in the (private) constructor of a Singleton. Otherwise, before accessing the global variable, you always need to check it's validity. :-/ – Peter Merkert Oct 23 '16 at 22:14
  • Thank you. Setting this as correct answer for suggesting to use a Singleton class, which is what I believe now I should do. – remi Oct 24 '16 at 07:04