I am trying to make a singleton with gcc
I found several examples on stackoverflow, unfortunately they do not work for me.
Here is my .h file:
#ifndef GLOBALINFO_H
#define GLOBALINFO_H
#include <string>
#include <iostream>
using namespace std;
class GlobalInfo
{
private:
GlobalInfo();
GlobalInfo(GlobalInfo const&);
GlobalInfo& operator=(GlobalInfo const&);
public:
static GlobalInfo& GetInstance();
virtual ~GlobalInfo();
bool isFullScreen;
int screenWidth;
int screenHeight;
string title;
protected:
};
#endif // GLOBALINFO_H
here is my .cpp:
#include "GlobalInfo.h"
GlobalInfo::GlobalInfo()
{
isFullScreen = false;
screenWidth = 800;
screenHeight = 600;
cout << "creating GlobalInfo" << endl;
}
GlobalInfo::~GlobalInfo()
{
}
GlobalInfo &GlobalInfo::GetInstance() {
static GlobalInfo instance;
return instance;
}
GNU C says:
include\GlobalInfo.h|12|error: 'GlobalInfo::GlobalInfo(const GlobalInfo&)' is private|
If i remove
GlobalInfo(GlobalInfo const&);
GlobalInfo& operator=(GlobalInfo const&);
from the header, i am getting a actual copy of the singleton, which is bad in this situation, as i use the isFullScreen to hold information depending on user input.
How do you actually use the singleton class? You are supposed to get a compile error when trying to invoke the copy constructor since the only method of getting the instance of the singleton should be via GetInstance. Perhaps you forgot to use the reference in the calling code:
If you forget the & operator you would effectively be attempting to copy the singleton, which correctly results in a compile error.