【问题标题】:How can i modify variables in static member function?如何修改静态成员函数中的变量?
【发布时间】:2019-07-17 06:50:10
【问题描述】:

我在下面有一段代码,我想在静态函数中修改类的变量,但出现了一些错误。 如何用“this”指针修复它?

类中的静态成员无法访问“this”指针,另一方面,我正在尝试访问静态成员函数中的类变量,因此我正在寻找一种使用“this”指针的方法“我”班的人来做。

class me {
  public:
     void X() { x = 1;}
     void Y() { y = 2;}

static void Z() {
  x = 5 ; y = 10;
}

public:
  int x, y;
};

int main() {
  me M;

  M.X();
  M.Y();
  M.Z();

  return 0;
}

我收到了这个error

在静态成员函数中对成员“me::x”的使用无效。

【问题讨论】:

    标签: c++ class static-methods class-members


    【解决方案1】:

    你有两种方法:

    • 如果您的成员用于static 方法,请将其定义为static
    • class's 成员是non-static 时,实现不要使用static 方法

    通常,static 成员或methods 的内存创建一次,即使您不创建class 的对象。所以你不能在static 方法中使用non-static 成员,因为non-static 成员仍然没有内存而static 方法有内存......

    试试这个:

    public:
       static void X() { x = 1;}
       static void Y() { y = 2;}
    
    public:
       static int x;
       static int y;
    

    别忘了初始化static成员:

    int me::x = 0;
    int me:y = 0;
    

    您不能在static 方法内使用this 指针,因为this 只能在non-static 成员函数内使用。请注意以下几点:

    this->x = 12;        // Illegal use static `x` inside a static method
    me::x = 12;          // The correct way to use of `x` inside a static method
    

    【讨论】:

    • 感谢您的回答,但我需要通过此代码执行此操作并使用“this”指针。
    • 类中的静态成员无法访问“this”指针,另一方面,我正在尝试访问静态成员函数中的类变量,因此我正在寻找一种方法使用类“me”的“this”指针来做。
    • @Baharmast 我改进了我的答案。请再次检查。我在静态方法中解释了this 指针...
    • 变量的初始化必须在cpp文件中。
    【解决方案2】:

    您可以将指向实例的指针传递给方法:

    class me {
    public:
        void X() { x = 1;}
        void Y() { y = 2;}
    
        static void Z(me* this_) { // fake "this" pointer
          this_->x = 5 ;
          this_->y = 10;
        }
    
    public:
        int x, y;
    };
    
    
    int main() {
        me M;
    
        M.X();
        M.Y();
        M.Z(&M);  // this works, but
        // usually you call static methods like this
        // me::Z(&M);
    
        return 0;
    }
    

    【讨论】:

    • 谢谢!这正是我的问题的答案
    【解决方案3】:

    您正在尝试使用来自静态成员函数的非静态成员。这就是为什么它给你一个错误。

    您可以通过使成员函数为非静态(通过删除 static 关键字)来解决此问题。您也可以将变量设为静态,以便您的静态成员函数可以访问它,但如果这样做,其他两个函数仍然无法编译。

    【讨论】:

      【解决方案4】:

      静态方法只能访问静态成员。

      class me {
      
        public:
      void X() { x = 1;}
      void Y() { y = 2;}
      
      static void Z() {
        x = 5 ; y = 10;
      }
      
      public:
        static int x, y;
      };
      
      int main() {
        me M;
      
        M.X();
        M.Y();
        M.Z();
      
        return 0;
      }
      

      【讨论】:

      • 感谢您的回答,但我需要通过此代码执行此操作并使用“this”指针。
      【解决方案5】:

      除了@nivpeled 说的,想想这个:

      您可以在您的程序中创建多个me 实例。静态Z() 方法应该修改哪些实例?

      【讨论】:

        【解决方案6】:

        我在这里为你提供终极解决方案,哈哈。

        我已经问过这个问题很多次了,但人们并没有思考或尝试解决问题,而是开始评判我认为有趣的设计。

        所以我必须解释一下,在某些情况下,您需要让静态成员访问非静态成员,例如当您在程序中使用脚本接口(例如 Lua)时,该接口只能访问静态类的成员,这些成员需要访问非静态的,无论如何让我们看看如何去做。

        class me {
          public:
             me() { This = this; } // Or you can assign it wherever you want
             void X() { x = 1;}
             void Y() { y = 2;}
        
        static me* This; // Here is our "this" pointer :P
        static void Z() {
          This->x = 5 ; This->y = 10;
        }
        
        public:
          int x, y;
        };
        
        //Remember to initialize it to avoid any linker's  errors
        me* me::This = nullpter; // or NULL or even 0
        
        int main() {
          me M;
        
        // now you can access "this" pointer the usually way
          M.X();
          M.Y();
          M.Z();
        
          return 0;
        }
        

        经过一段时间的思考,这是我找到的最有效的解决方案。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-09-07
          • 2011-11-12
          • 1970-01-01
          • 2020-08-09
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多