How can i modify variables in static member function?

烈酒焚心 提交于 2019-12-31 05:29:13

问题


I have a code below, i want to modify class's variables in static function but there is some error. How can i fix it with "this" pointer?

There is no access to "this" pointer for static members in class,on the other hand I am trying to make an access to class variables in Static member function, therefore i am looking for a way to use "this" pointer of class "me" to do it.

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

I got this error :

invalid use of member ‘me::x’ in static member function.


回答1:


You can pass a pointer to an instance to the method:

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



回答2:


You have two ways to do it :

  • Define your members as static if they are used in a static method.
  • Implement Dont use of static methods when class's members are non-static

Generally, the memory of static members or methods created once even when you dont create an object of your class. So you cannot use of a non-static members in a static method, because non-static members still have no memory While static methods have memory...

Try this :

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

public:
   static int x;
   static int y;

Dont forget to initialize static members :

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

You cannot use of this pointer inside a static method, because this may only be used inside a non-static member function. Notice the following :

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



回答3:


You are trying to use a non-static member from a static member function. That's why it's giving you an error.

You can fix this by making the member function non-static (by removing static keyword). You could also make the variable static, so that your static member function can access it, but if you do so, the two other functions will still not compile.




回答4:


Static method can access static members only.

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



回答5:


Adding to that @nivpeled said, think about this:

You may create several instances of me on your program. Which ones of the instances the static Z() method should modify?




回答6:


I have the ultimate solution for you here lol.

I've asked this question so many time but instead of thinking or trying to solve the problem people starts judging my design which I think is funny.

So I have to explain that in some situations you will be in need to make static members gain access to non static ones, such case when you use a scripting interface in you program(Lua for example) that interface can only access static members of a class, which those members need to access to non static, anyways lets see how to do it.

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

After a while of thinking this is the most efficient solution i found.



来源:https://stackoverflow.com/questions/57070060/how-can-i-modify-variables-in-static-member-function

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