Please have a look at the following code
UIHandler.cpp
#include "UIHandler.h"
#include <iostream>
using namespace std;
UIHandler::UIHandler()
{
}
UIHandler::~UIHandler(void)
{
}
UIHandler *UIHandler::getInstance()
{
if(uiHandler==NULL)
{
uiHandler = new UIHandler();
}
return uiHandler;
}
UIHandler.h
#pragma once
class UIHandler
{
public:
~UIHandler(void);
static UIHandler *getInstance();
private:
UIHandler *uiHandler();
UIHandler();
};
I am new to C++ and I am trying to implement the singleton pattern here. But, this one is giving errors! It says “expression must be a modifiable lvalue“, in the place uiHandler = new UIHandler();
Why is this? Please help!
UIHandler *uiHandler();declaresuiHandleras a method, not a data member. Change it toThe
staticis there because you’re accessing it from astaticmethod.Note that a better way would be
and just get rid of the member.
Don’t forget to disallow copying.