Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 11-08-2003, 04:20 PM   #1
Registered: Oct 2001
Distribution: Gentoo
Posts: 139

Rep: Reputation: 15
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?
Old 11-15-2003, 03:50 PM   #2
Senior Member
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
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).
Old 11-16-2003, 12:07 PM   #3
Registered: Nov 2003
Location: Ronneby, Sweden
Posts: 555

Rep: Reputation: 30
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):
class MySingletonClass

	// Singleton pattern
	static MySingletonClass *Instance();
	void AnyOtherMethod();


	//Hide constructor and destructor from public

}; //class MySingletonClass
The class definition (.cc-file):
MySingletonClass *MySingletonClass::Instance()
	// Since the variable is static, this statement will only be
	// executed once, the first time execution reaches this point
	static MySingletonClass *sing = new MySingletonClass();

	return sing;
} //MySingletonClass *MySingletonClass::Instance()
Usage of the instance:
#include "MySingletonClass.hh"

// The Instance() method is static and can be accessed anywhere
// without any object reference.
Good luck with your project!
Old 11-16-2003, 05:00 PM   #4
Registered: Oct 2001
Distribution: Gentoo
Posts: 139

Original Poster
Rep: Reputation: 15
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.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
I like global warming. slapNUT General 177 12-12-2006 05:04 AM
C Global Declaration Aju Programming 3 09-29-2005 05:21 AM
WAS Global Security haobaba1 Programming 0 03-11-2004 09:19 PM
C: global typedefs dominik81 Programming 1 11-30-2003 03:34 PM
Global Settings mikeshn Linux - Software 1 07-22-2003 08:42 PM

All times are GMT -5. The time now is 01:42 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration