从静态函数调用非静态变量 [英] Call non-static variable from a static function

查看:1926
本文介绍了从静态函数调用非静态变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用C ++,我偶然发现了一个我似乎不能理解的问题。

  MyClass 
{
bool eventActive = false;

static bool JoinCommand()
{
if(eventActive == true)// eventActive错误:非静态成员引用必须相对于特定对象
{
//在这里做某事...
}
else
{

}
};

我需要 JoinCommand 还需要使用 eventActive ,它需要是非静态的,并且由同一类中的其他函数使用/修改。因此,我不能使 eventActive static,因为我将需要使其const aswell。



$ b非静态成员引用必须是相对于特定对象



我想我不能创建一个新的实例 MyClass 。所以我应该怎么处理这个?还是有什么错误,我不明白/我误会?非常感谢任何帮助。






编辑:



感谢大家帮助我在这一个。我走到了我想达到的目标,但学到了新的东西,我偶然发现了另一个接近这个问题的问题。

  class Command 
{
public:
const char * Name;
uint32权限;
bool(* Handler)(EmpH *,const char * args); //我不想通过添加更多的参数改变这种情况
};
class MyClass:public CommandScript
{
public:
MyClass():CommandScript(listscript){}
bool isActive = false;
命令* GetCommands()const
{
static Command commandtable [] =
{
{showlist,3,& DoShowlistCommand} //可能处理不同的是解决我下面提到的问题?
};
return commandtable;
}
static bool DoShowlistCommand(EmpH * handler,const char * args)
{
//我需要在IF语句中使用isActive,但不能是
// DoShowlistCommand是静态的,isActive不是静态的。
//我不能将它作为参数传递,因为我不想
//改变所有类的命令结构

//有办法它?
}
};


解决方案

类的静态成员函数根据定义是独立的的类的任何非静态成员的状态。这意味着你可以使用它没有对象。



只要您依赖于非静态成员,您的函数就必须是非静态的:

  class MyClass 
{
bool eventActive = false;
public:
bool JoinCommand()//非静态,因为
{
if(eventActive == true)//它明显取决于对象的状态
{/ * do something here ... * /}
}
}

然后,您可以按预期调用您的成员函数:

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

如果您仍然有一个有效的理由保持您的函数静态: / p>

在静态成员函数中访问非静态元素的唯一方法是告诉函数它必须使用哪个对象来获取访问非静态(参数,全局对象,等等...)。 Exacltly你会抛弃的类。



示例:

  class MyClass 
{
bool eventActive = false;
public:
static bool JoinCommand(MyClass& omc)//示例通过提供参数
{
if(omc.eventActive == true)//访问非静态object
{/ * do something here ... * /}
}
};

您可以这样调用:

  MyClass obj; 
if(MyClass :: JoinCommand(obj))//有点笨拙,不是吗?
...


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

解决方案

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 ? 
   ...

这篇关于从静态函数调用非静态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆