I've recently started working with C++ and I have stumbled across a problem that I cannot seem to understand.
class MyClass
{
bool eventActive = false;
static bool JoinCommand()
{
if (eventActive == true) // eventActive error: "a nonstatic member reference must be relative to a specific object"
{
// do something here...
}
else
{
}
};
I need JoinCommand
to be static but I also need to use eventActive
which is required to be non-static and is used/modified by other functions in the same class. Therefore I cannot make eventActive
static because I will need to make it const aswell.
And the error: "a nonstatic member reference must be relative to a specific object"
I guess I cannot create a new instance of MyClass
. So how am I supposed to deal with this? Or is there anything wrong that I do not understand / I misunderstand? Any help will be greatly appreciated.
EDIT:
Thanks to everyone for helping me out on this one. I made my way through to what I wanted to achieve but learning new things I stumbled accross another problem that is kind of close to this one.
class Command
{
public:
const char * Name;
uint32 Permission;
bool (*Handler)(EmpH*, const char* args); // I do not want to change this by adding more arguments
};
class MyClass : public CommandScript
{
public:
MyClass() : CommandScript("listscript") { }
bool isActive = false;
Command* GetCommands() const
{
static Command commandtable[] =
{
{ "showlist", 3, &DoShowlistCommand } // Maybe handle that differently to fix the problem I've mentioned below?
};
return commandtable;
}
static bool DoShowlistCommand(EmpH * handler, const char * args)
{
// I need to use isActive here for IF statements but I cannot because
// DoShowlistCommand is static and isActive is not static.
// I cannot pass it as a parameter either because I do not want to
// change the structure of class Command at all
// Is there a way to do it?
}
};