PDA

View Full Version : Creating a global array in my code???



therealjag
19th February 2006, 02:24
hey there i have created a global array in my file but when i try to access it (i.e. array[0] = 25), the program crashes when i run it. ive heard of a method called a singleton pattern?? is this the best way to go about creating a global array in my code? any help or advice would be much apreciated, thanks. Jag

yop
19th February 2006, 02:31
ive heard of a method called a singleton pattern??
http://www.qtcentre.org/forum/showthread.php?t=465 Avoid global variables if possible

brcain
23rd February 2006, 21:56
The Singleton pattern is for making sure a class only has one instance ... and can be accessed globally. You could let a class manage this data for you and possible encapsulate the type.

IMHO arrays are a error-prone way to manage data. Use a std::vector instead.

Do a search ... you'll find numerous online examples of the Singleton pattern. Here's the book from GoF:

Design Patterns
ISBN 0-201-63361-2
by Gamma, Helm, Johnson, Vlissides

dublet
3rd March 2006, 11:43
How exactly have you created it?

brcain
10th March 2006, 00:06
How exactly have you created it?

It's created when you call SingletonClass::getInstance() -- which is a class-scope method (static method in C++).

dublet
13th March 2006, 12:13
It's created when you call SingletonClass::getInstance() -- which is a class-scope method (static method in C++).
I was referring to the global array, not the singleton. :)