Global access in C++
In my program I need to create a collection(which is a object) of objects when the program starts. I get all the data for the objects from a flat files I create when the program closes.
Now since my program will have a GUI, created with gtkmm, I need to be able to minipulate this collection. However, I don't have access to them from the widget functions. How can I do this? |
I don't follow. Why don't you have access to something from some functions? From where you have the access? (Sorry for being stupid, I really have no experience on gtk-libraries; only C++ and programming in general).
|
I think I know what you mean. This is accomplished by a pattern known as Singleton. With a singleton class you get a class that exists in exactly one instance and every component of your program can access that same object. Is this what you need? Then look at this:
The class declaration (.hh-file): Code:
class MySingletonClass Code:
MySingletonClass *MySingletonClass::Instance() Code:
#include "MySingletonClass.hh" Martin |
Ya I think the singleton pattern is exactly what I need. As soon as I read that word I remebered it from one of my OO clases from last year, I had completly forgot about it.
Currently I have it just setup as a global variable, which I know is bad. Once I get my program a little further I'll have to start using it with the singleton pattern. I should really setup a little web site so that I can show you guys my progress. That way you can see where all you help is creating. |
All times are GMT -5. The time now is 06:34 AM. |