I have a function which does the following:
- When the function is called and passed a true bool value, it sets a static bool value to true
- When the function is called and passed a string, if the static bool value is set to true, it will do something with that string
Here is my concern — will a static variable remain the same between two overloaded functions? If not, I can simply create a separate function designed to keep track of the bool value, but I try to keep things simple.
No, it creates two separate static variables – one for each function. The name of any C++ function is made op of its apparent name and its parameter types, and the name of the static is (conceptually at least) tacked on to that. Rather than add yet another function, you could consider making the variable static with respect to the class containing the functions, although this does not give you exactly the same behaviour, or place it in an anonymous namespace:
To make the functions members of a class: