Recently I was going through a bunch of old game's source code and I found some things I don't quite understand about how it works. I am aware this is used in a lot of projects, but for the longest time I've been trying to figure it out with no luck.
They're just inside the .CPP file not within a function or class.
I know quite a bit about C++ programming, pointers, syntax, etc and have been working on a 2D Game Engine using SFML and a combination of other things.
I understand methods, classes, callbacks, static functions. Using preprocessor directives, etc.. but this is rather strange because it doesn't seem to be a method.
Can someone give me an Explanation on how these are used and implemented? The more details the better it will help me understand. Thank you!
CS_LEAKGUARD_IMPLEMENT is probably a macro (#define) that expands to a class definition or something. Of course I can't say for sure without seeing the actual macro definition.
Macros were used like this in older C++ before templates became more accepted. This line here might be similar to a template instantiation.
CS_IMPLEMENTATION is another macro... but this time it's either defined as nothing (ie just: #define CS_IMPLEMENTATION ) or is defined as a calling convention... similar to WinAPI's 'CALLBACK'.
Calling conventions tell the compiler how the parameters for the function should be arranged on the assembly level. This can be important for linking with other libraries/programs since if the parameters are arranged differently, calling the functions will not work as expected across the lib boundary. Explicitly specifying the calling convention ensures that the parameters will be arranged consistently.
The rest of that (sans the CS_IMPLEMENTATION) is just a normal function prototype/definition.
yup that's exactly what I thought. It's the macro version of what now would be written as a template.
EDIT:
C still does this kind of stuff because it doesn't have templates... but this has ctors/dtors so it's clearly C++. Either this code is old or the developers just didn't know how to use templates properly (or couldn't use them due to environment restraints).