C++ 如何修改静态成员函数中的变量?

C++ 如何修改静态成员函数中的变量?,c++,class,static-methods,class-members,C++,Class,Static Methods,Class Members,我有下面的代码,我想在静态函数中修改类的变量,但有一些错误。 如何使用“this”指针修复它 类中的静态成员无法访问“this”指针,另一方面,我试图访问静态成员函数中的类变量,因此我正在寻找一种使用类“me”的“this”指针的方法 class me { public: void X() { x = 1;} void Y() { y = 2;} static void Z() { x = 5 ; y = 10; } public: int x, y; };

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

类中的静态成员无法访问“this”指针,另一方面,我试图访问静态成员函数中的类变量,因此我正在寻找一种使用类“me”的“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;
}
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;
}
我得到了这个
错误

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


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

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

有两种方法可以做到这一点:

  • 如果在
    静态
    方法中使用成员,则将其定义为
    静态
  • 类的
    成员是
    非静态
通常,即使您没有创建
的对象,也会创建一次
静态
成员或
方法的内存。因此,您不能在
静态
方法中使用
非静态
成员,因为
非静态
成员仍然没有内存,而
静态
方法有内存

试试这个:

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

public:
   static int x;
   static int y;
不要忘记初始化
静态
成员:

int me::x = 0;
int me:y = 0;
不能在
静态
方法内使用
指针,因为
只能在
非静态
成员函数内使用。请注意以下事项:

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

您正试图使用静态成员函数中的非静态成员。这就是为什么它会给你一个错误


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

除此之外@nivpeled还说,请考虑以下问题:


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

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

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

我这里有你的终极解决方案,哈哈

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

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

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

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

谢谢你的回答,但我需要用这段代码并使用“this”指针。类中的静态成员无法访问“this”指针,另一方面,我试图访问静态成员函数中的类变量,因此,我正在寻找一种方法来使用类“me”的“this”指针来完成它。@Baharmast我改进了我的答案。请再检查一遍。我在静态方法中解释了
这个
指针…变量的初始化必须在cpp文件中。谢谢你的回答,但我需要用这个代码并使用“这个”指针。谢谢!这正是我问题的答案