non-static variables

Started by
7 comments, last by jflanglois 18 years, 9 months ago
Hi, how do I use non-static variables from within a static function? Scanmaster_K
Advertisement
I assume you mean a class variable from a static method within that same class?

You don't directly. You could pass a pointer or reference to the class, but if you are doing that, then you may have a design problem. What are you trying to do exactly?

[edit] Also, what language is this?
Well in a static function Im trying to change the value of a non-static bool (class variable).

I use C++
I'll give you an example first:
class test {  public:    static void func( test &p ) {      p.i = true;    }  private:    bool i;};int main() {  test t;  test::func( t );  return 0;}

But I would like to know why you are using a static method to modify a private data member. There might be a better way of doing this.


jfl.

[Edited by - jflanglois on July 10, 2005 4:40:11 AM]
I use it as a function pointer when I use SDL_AddTimer.
I see. I have never used SDL, but it seems that that might be the way to go, then. You can cast the void * for your callback to the class that you are trying to access and go from there:
typedef unsigned Uint32;class TimedClass {  public:    static Uint32 Callback( unsigned interval, void *param ) {      reinterpret_cast< TimedClass * >( param )->b = interval;      return 0;    }  private:    unsigned b;};typedef Uint32 ( *SDL_NewTimerCallback )( Uint32 interval, void *param );// The following is just a stub SDL_AddTimer function, since// I don't have SDL installed. The real SDL_AddTimer returns// a SDL_TimerID anyway.void SDL_AddTimer( Uint32 delay, SDL_NewTimerCallback callback, void *param ) {  callback( delay, param );}int main() {  TimedClass t;  SDL_AddTimer( 20, TimedClass::Callback, &t );  return 0;}


You might also like to look at this for a more OO way of dealing with the problem (though the article talks about threads, the concept is applicable to any function that you want to OO-ize).

jfl.

[edit] Made the example more applicable.
Perhaps it would help if I explained the difference between static and non-static member functions.

When you call a non-static member function on a class instance, you secretly pass in the instance to the function, and the function then uses that class instance to access its member variables, for example.

class eg{  void somefunction()  {    std::cout << b_;  }bool b_;};


Here is an example of a non-static member function that prints out the member variable b_. Only one version of the somefunction() function exists in the program, and is used by all instances of eg. So how does the non-static member function know which instances b_ variable to print out? The answer is the 'this' pointer, the above code is translated to something along the lines of.

class eg{void somefunction(eg* this){std::cout << this->b_;}...};


The 'this' pointer refers to the instance of the class that the function is being called on. This gives the compiler a way to determine which instance of the member variable belongs to the instance of eg that the function is being called on. When the function is called.

eg instance;instance.somefunction();



This would be translated into something like

eg::somefunction(&instance);


Which is similar to a static function call. So to answer your question, in order to access the non-static variable b_ from a static member function, you need to emulate the 'this' pointer by yourself, which involves passing the static function a pointer or reference to the instance that has the variable you wish to access.

class eg{static void somestaticfunction(eg* emulatedthis){std::cout << emulatedthis->b_;}bool b_;};


[smile]
It works, thanks alot.


Scanmaster_K
You're welcome.
[edit] Good explanation Jingo, btw.

jfl.

This topic is closed to new replies.

Advertisement