|
-
December 27th, 2007, 06:41 PM
#2
Re: C++ and global objects
 Originally Posted by Sk#
1) since the right thing to do would be to enclose most of the code inside of a class (including a singleton object of an Application class), I still need to specify a pointer to the function to be used as a callback, and this function should be a member function, which is hard to do (I found a couple of techniques, but I can't convince myself that they're worth the pain);
Can you show us what you did? I do not know GLUT, so I'm surmising that the callback function must be a static member function if it is to be used within a class.
The way this is usually done if the function must be static is that the static function finds the object that the callback is associated with, and then calls the object's Paint, Keyboard, whatever function. These other functions are non-static members. The way that the callback searches for the correct object -- there are many ways that it could do it.
2) the usual approach I used in C was to create a "global" header file, which I would include in the main, and which would declare all the global variables (for example, the data structure that will contain the map of the world, since it will have to be available in multiple callback functions). Now, if I declare some objects this way (say, in "mainlib.h") and then include the header in more than one source file, the linker says that the object had already been declared in another object file.
That is because for global variables that are shared across source units, only one source unit declares the variable, and the other source units must use the extern keyword.
But there is no need for globals. This is not the best thing, but as a start, just put those globals in a class as static members. Then you can include that class in as many places as you want and not have any linker errors. Of course, you have to change your source code to say "MyGlobals::whatever" instead of "whatever", since they are all static members of a class.
Regards,
Paul McKenzie
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
|