there. I am confused about what the use of the & symbol between the classname and variable name in a c++ #define statement.
I found it in the globalhandling.hpp file in the xbmc source code to create a global singleton.
I wrote a similar version of the snippet to figure out what it does. What I found when experimenting with it is when & is used only one constructor and destructor is called. If I omit it one constructor and two destructors is called.
Is & acting as a bitwise and or an address operator in this context?
#include <iostream>
#include <boost/shared_ptr.hpp>
using namespace std;
class aClass
{
public:
aClass() { cout << "Constructor\n"; }
aClass getInstance() { return *this; }
void printMessage() { cout << "Hello\n"; }
~aClass() { cout << "Destructor\n"; }
};
#define GLOBAL_REF(classname,variable) \
static boost::shared_ptr<classname> variable##Ref(new classname)
#define GLOBAL(classname,variable) \
GLOBAL_REF(classname,variable); \
static classname & variable = (*(variable##Ref.get()))
GLOBAL(aClass,aVariable);
int main()
{
aVariable.printMessage();
return 0;
}