Jump to content
  • Advertisement
Sign in to follow this  
donjonson

if you overload a function that has static variables

This topic is 4773 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

say I have a function with two overloads. one with arguments and the other with no arguments. the one with arguements saves the arguements as the statics. and the one without the arguements accesses the arguements from the statics. does that work? of cource the one with arguements must be run first.

Share this post


Link to post
Share on other sites
Advertisement
no. If you have a static variable inside of a function, it is local to that function. Overloaded functions are entirely seperate, the only thing they have in common is the name.

Share this post


Link to post
Share on other sites
why not make the function with arguments a general one, then have the overloaded no-argument one call it. The speed decrease will be negligible if you inline the no-argument one.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Another solution would be to create a function that has a default value for the parameters and if that defalut value is used the function will initialize the static variable otherwise it will use that assigned value.



//.h file

int GetStaticValue(int param = 0);


// .cpp file
int GetStaticValue(int param){
static int staticvalue;
if (param == 0){
staticvalue = 20;
}
return staticvalue;
}

// main.cpp
...
GetStaticValue() // since no parameters were given it will use the default value of 0.
int result = GetStaticValue(2);
...


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!