I'm attempting my first bigger C++ object and I'm running into a bit of a problem when it comes to implementing a singleton using a shared pointer.
When I try to compile the following, I Visual Studio gives me this error:
"Error C2248 'PaletteManager::PaletteManager': cannot access private member declared in class 'PaletteManager' {omitted}\xmemory line 228"
I'm guessing the issue is because I have a private constructor/destructor and make_shared is trying to call the constructor. This access problem makes sense to me, but if I want to use a shared pointer as the way to access my singleton object, what am I supposed to do? The code works just fine with a raw pointer, but I wanted to try and do things the clean way with a smart pointer.
Here is the header file for the code in question:
class PaletteManager
{
private:
// array representing palette colors
uint* paletteColors;
// private constructor/destructor because singleton
PaletteManager();
~PaletteManager();
// load palette from file TODO: not implemented
void loadPallette();
static std::shared_ptr<PaletteManager> instance;
public:
const uint PALETTE_MAX_COLORS = 4;
uint getPaletteColor(uint idx);
// singleton accessor
static std::shared_ptr<PaletteManager> getInstance();
};
And here is the function at issue in the cpp file:
std::shared_ptr<PaletteManager> PaletteManager::instance = nullptr;
std::shared_ptr<PaletteManager> PaletteManager::getInstance()
{
if (!PaletteManager::instance)
{
PaletteManager::instance = std::make_shared<PaletteManager>();
}
return PaletteManager::instance;
}