08-01-2006, 01:46 AM,
|
|
thelusiv
Administrator
      
|
Posts: 2,346
Threads: 223
Joined: Jun 2005
|
|
Code cleanup branch
I've been doing a little work on a new branch called code-cleanup. You can check it out like this:
Code: svn co http://svn.vdrift.net/repos/vdrift/branches/code-cleanup vdrift-code-cleanup
At the moment this will nearly compile but throws tons of linker errors. Essentially what I've done is moved all the global variables and functions from main.cpp into a class called VGAME.
Now, all the functions in all the other classes that used to use those globals are unable to find the functions they need...so something will have to replace them. I suspect I will make some of the functions and variables in VGAME public so that the functions can be called and the variables can be used as they used to by just following a reference to the VGAME object. But I guess that means it has to be global....sigh. I guess I could code data return paths into the various places that need them like I did with the Gui class. Hmmm...I think I'll sleep on it. If anyone has any ideas about how I should make the globals not-global as gracefully as possible, speak up.
|
|
08-01-2006, 05:34 AM,
|
|
FFuser
Member
  
|
Posts: 147
Threads: 10
Joined: Jul 2005
|
|
can you make the functions in VGAME static (maybe it is not static but somethiing else)?
so you could use VGAME::functionName ();
|
|
08-01-2006, 11:38 PM,
|
|
alex25
Senior Member
   
|
Posts: 531
Threads: 42
Joined: Jun 2006
|
|
thelusiv Wrote:As private members of the VGAME class, the former globals are totally inaccessible. I could make them public, or provide a public function to return a reference or something along those lines. However it seems to me that will make it harder to update all those references to the globals.
i haven't looked at the code yet, but you can always make the data protected and make the classes that need to update the data friends of the VGAME class. this way you can control how the global data is being accessed. i'll try to take a look at the code soon.
--alex--
|
|
08-04-2006, 01:20 AM,
|
|
matthew_i
Junior Member
 
|
Posts: 22
Threads: 1
Joined: Dec 2005
|
|
You don't always have to do the GetInstance() thing. You can make GetInstance return a reference then use that:
my_type& t = my_type::instance()
t.stuff()
Then it works very much like a global. The thing I like about singletons is they are created on first use *after* main() has started executing. You don't have to initilize them twice like globals. Also they encapsulate data better than globals do, for example, you don't generally access one singleton from another singleton.
Functionally, both globals and singletons serve pretty much the same purpose, but I think singletons are a better, more complete, and more robust solution.
|
|
08-04-2006, 01:20 AM,
|
|
matthew_i
Junior Member
 
|
Posts: 22
Threads: 1
Joined: Dec 2005
|
|
You don't always have to do the GetInstance() thing. You can make GetInstance return a reference then use that:
my_type& t = my_type::instance()
t.stuff()
Then it works very much like a global. The thing I like about singletons is they are created on first use *after* main() has started executing. You don't have to initilize them twice like globals. Also they encapsulate data better than globals do, for example, you don't generally access one singleton from another singleton.
Functionally, both globals and singletons serve pretty much the same purpose, but I think singletons are a better, more complete, and more robust solution.
|
|
08-04-2006, 01:41 AM,
|
|
matthew_i
Junior Member
 
|
Posts: 22
Threads: 1
Joined: Dec 2005
|
|
No, I don't really recommend having a reference as a memeber for the class, although you could do it that way. I think it would be better to have a reference for each function that uses the singleton rather than one per class. It would work fine both ways though...
|
|
08-04-2006, 01:41 AM,
|
|
matthew_i
Junior Member
 
|
Posts: 22
Threads: 1
Joined: Dec 2005
|
|
No, I don't really recommend having a reference as a memeber for the class, although you could do it that way. I think it would be better to have a reference for each function that uses the singleton rather than one per class. It would work fine both ways though...
|
|
|