Reputation: 83
I am trying to make a program where each form's class needs access to a hash table. The hash table is only created once throughout the life of the program. I was planning on just passing the address of the hash table to a setter function within each class. Is there a better way of doing this without introducing global variables?
Upvotes: 1
Views: 269
Reputation: 1
Try opaquing your hash table in singleton pattern with all the features that it gives you.
Than you only need to link to hash table class, you won't need it to be global.
Upvotes: 0
Reputation: 13510
This is the way, but I must say that there are cases when using global vars is just fine, and you should not be "over afraid" of using them, and I mean the cases of resources.
For example, the file system is global, you don't pass a file system object to your classes, but rather access the file system using global functions, fopen, fread, etc...
The same goes for the printer.
The same goes for an application database.
And perhaps your hashtable is also that kind of resource, which you can access globaly.
If you access it from multiple threads it's best to create access functions that apply some synchronization (semaphore, etc..).
Upvotes: 1