Jump to content
  • Advertisement
Sign in to follow this  
nickwinters

static variables within object methods

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

If I have some class, A, and within A, I have some function Blah, and the following code
A::Blah()
{
  static int temp;
}
now the variable temp is static for the function, but also shares the same value for all instances of A. Is there a way to make it static for the function, but have a different value for each instance? Thanks. -Nick

Share this post


Link to post
Share on other sites
Advertisement
Hmm. Not that I can think of right now...

At first I thought maybe just make it static to the class, but that wouldn't help either.

Your best bet is to just make it a non-static member of the class, and try to only initialize it once.

Share this post


Link to post
Share on other sites
Why not just have:



class A
{
private:
int temp;

public:
void Blah()
{
temp++; // or whatever
}
}




I really don't understand your purpose, you want a static-nonstatic variable?

Share this post


Link to post
Share on other sites
Actually, you're right... There is no purpose for that. I still need it to talk between two instances.

Oh well, thanks anyways.

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!