LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices



Reply
 
Search this Thread
Old 11-08-2003, 04:20 PM   #1
jpc82
Member
 
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
ToniT
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
MartinN
Member
 
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):
Code:
class MySingletonClass
{
public:

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

private:

	//Hide constructor and destructor from public
	MySingletonClass();
	~MySingletonClass();

}; //class MySingletonClass
The class definition (.cc-file):
Code:
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:
Code:
#include "MySingletonClass.hh"

// The Instance() method is static and can be accessed anywhere
// without any object reference.
MySingletonClass::Instance()->AnyOtherMethod();
Good luck with your project!
Martin
 
Old 11-16-2003, 05:00 PM   #4
jpc82
Member
 
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.
 
  


Reply


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 10:27 PM.

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