Call non-static variable from a static function

风格不统一 提交于 2019-12-24 15:26:40

问题


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?
  }
};

回答1:


A static member function of a class is by definition independent of the state of any non static member of the class. This means that you can use it without an object.

As soon as you rely on a non static member, your function has to be non static:

class MyClass
{
    bool eventActive = false;
public: 
    bool JoinCommand()           // non static, because 
    {
        if (eventActive == true) // it depends clearly on the state of the object
        { /* do something here... */ }
    }
};

You can then call your member function as expected:

MyClass omc; 
if (omc.JoinCommand()) 
     cout << "Command joined successfully"<<endl;  

If you have nevertheless a valid reason to keep your function static:

The only way to access non static elements, in a static member function is to tell the function which object it has to use for getting access non statics (parameter, global object, etc...). Exacltly as you would do oustide the class.

Example:

class MyClass
{
    bool eventActive = false;
public:
    static bool JoinCommand(MyClass& omc)  // example by provding a parameter
    {
        if (omc.eventActive == true)  // access non static of that object
        { /* do something here... */ }
    }
};

You would call it like this:

MyClass obj; 
if (MyClass::JoinCommand(obj))    // a little bit clumsy, isn't it ? 
   ...



回答2:


If you actually want JoinCommand to be a static method, while eventActive is a non-static member, your only choice is to pass in the object to the method explicitly.

class MyClass
{
    bool eventActive = false;

    static bool JoinCommand(MyClass *object)
    {
        if (object->eventActive) // No error, since we are referencing through an object.
        {
           // do something here...
        }
        else
        {
           // Do something else here
        }
    }
};



回答3:


Having a non-static member variable means that each instance of MyClass has an independent eventActive member, and that the value can be different for each instance. This second point means that something like "MyClass.eventActive" doesn't have any meaning. What if you have two instances of MyClass, and the value of eventActive is true in one and false in the other? Which value do you mean? If you're trying to make a class for which there can only be one instance, you might want to take a look at the singleton pattern. Even in that case though, JoinCommand would have to be non-static.



来源:https://stackoverflow.com/questions/35370901/call-non-static-variable-from-a-static-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!