Jump to content
  • Advertisement
Sign in to follow this  
Scanmaster_k

non-static variables

This topic is 4820 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!