|
-
August 9th, 2009, 01:53 PM
#12
Re: Theory Question: Global Variables
Until you actually learn classes, you can simulate a class object using global variables if:
1) You clearly demarcate a set of related functions which will use each global, and allow no others to do so.
2) If the set of functions needing access to a value is not conceptually groupable, consider whether writing a getVarName() function which offers read-only access to a global allows you to limit the set of directly-accessing functions to the point where they do form a clear conceptual group of operations. (Read-only access to a global int, for instance, could be achieved by returning a "const int&" from the function. Likewise for any type.)
3) Your conceptual function grouping can be thought of as a single object, machine, or agent in the system. Don't worry about precise definitions----it just needs to represent a "thing which does stuff." It should be conceptually possible to have more than one of these things, but you cannot support actually having more than one until you start using classes.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|