I’m pretty new to C++ language. I’m using it with Qt to create a cross-platform user interface.
The user interface will show different graphs and plots by using the same data. So I would like to have my data stored in a class (Data_Class) accessible from all the other classes.
In the data class, I am using private static variables and public static methods (get/set).
What is not clear to me is how to access the data class from the other classes. In other words, how to get data values in Class_B (from Data_Class) after I write them in Class_A (to Data_Class).
Assuming that this is syntax an oriented question:
In
ClassA:Don’t forget to add
Data_Classstatic members definitions, for example intodataclass.cpp:Otherwise you’ll get an
undefined referencelinker error.You also may consider using singleton pattern (according to comments, singleton is bad idea and you should avoid it if possible) as Aubin suggested which will take care of adding new static member definitions:
Usage:
Or go with little more complex but more flexible Dependency injection which is according to wiki: