Chogart
Chogart

Reputation: 84

Calling a non-static variable from a static function

I encountered an issue while trying to do something in the process of learning C++ and I am not sure how to handle the situation:

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

Any help would be greatly appreciated! :)

Upvotes: 1

Views: 1285

Answers (3)

Christophe
Christophe

Reputation: 73376

There are two potential answers here:

1. about use of non static items in a static functions:

As said in our previous question/answer, this is not possible, unless you'd have in the static function a specific MyClass object (and use object.isActive). Unfortunately, you can't do this here :

  • your code comments clearly show that you can't add a MyClass parameter to the function call;
  • the existing parameters don't suggest that you have already a pointer to parent class object;
  • it would not be adivsable to use global objects in such a context.

2. about what your're trying to do:

It seems that you want to have the function static, because you want to provide it in a table that maps script-commands to function pointers.

Alternative A

If all the function pointers used in commandtable are members of MyClass, you could think of using a pointer to a member function instead of a pointer to a function. The outside object/function that sets isActive on an object, could then refer the pointer to the member function, on the MyClass object it knows.

Alternative B

Revise the design of your code to implement your script engine by using the command design pattern: it's ideally suited for this kind of problems. It will require some refactoring of your code, but it will be so much more maintenable and extensible afterwards !

Upvotes: 2

Liza
Liza

Reputation: 103

I don't think there is any way to do it. Here is why: A static member function is not attached to any particular object, which means it cannot access other members that are not static, since they are attached to an object.

It doesn't look like you need to make it a static member. If you are sure you do - then pass it as a parameter. For example, make a

bool isActive();

function, and pass an argument from it to that function somewhere when you call this 'problematic' one.

You also could change your member variable to static, but it looks like you need it for EACH object, not one-for-all

Upvotes: 2

Emil Laine
Emil Laine

Reputation: 42838

// Is there a way to do it?

No.

Either pass it as parameter, make it static, or make DoShowlistCommand non-static.

Upvotes: 3

Related Questions