views:

102

answers:

3

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.

+6  A: 

Two overloaded functions are two different functions. Even if each function contains a static bool with the same identifier, they belong in different scopes and the identifier refers to a distinct variable in each function.

If you need to share state between two functions you are probably better off making a class to encapsulate this state and making the two functions member functions of this class.

Charles Bailey
I initially began with the creation of a class. However, this would require that I constantly create a new object of that class for each function throughout my program. Since I need to be able to seamlessly use these functions, I don't see anyway to get around this without creating a new object in each function or passing the object between functions (both very tedious).
BSchlinker
@BSchlinker: The functions can be static. In this case, you don't need to create any object.
Gorpik
@Gorpik What is a static function? I don't see how this would help in this scanario.
BSchlinker
You need to put this object in a scope that is suitable for its use. If functions need to use the object then they need to be passed the correct instance. If it truly is a global object then you _could_ make it a namespace scope variable and let everything access it directly.
Charles Bailey
@Gorpik: If you mean the functions can be class static, then no, this is not what I meant. If they were static they wouldn't have access to the state encapsulated by the class object so this isn't a solution.
Charles Bailey
@BSchlinker: I'm not sure I understand. To put a variable at namespace scope you just declare directly in a namespace. `namespace ns { MyObject obj; }` What sort of example do you need?
Charles Bailey
@Charles Bailey: Just make the `static bool` value also a static member of the class.
Gorpik
@Gorpik: But now you have a class with only static methods and static data and you don't have any encapsulation and you have no choice about limiting the lifetime and scope of the data.
Charles Bailey
@Charles Bailey: Originally we had just global functions with static variables inside. Now we have public static class functions with a private static class variable. I don't see where have we lost any encapsulation, and we never had any limits to lifetime or scope.
Gorpik
@Gorpik: I know. I was suggesting an improvement; your comments were undermining any improvement in my suggestion.
Charles Bailey
@Gorpik: a class with only static members is really abusing the concept of classes (but c++ does give you that ability). Doing it this way is no better than having a set of functions and a global variable as basically you are tightly coupling the code. Charles suggestion of using a class instances allows you (at a later time) to easily decouple the code from any particular instance as you can then inject a reference into the code thus decoupling it.
Martin York
+3  A: 

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:

namespace {
   int myvar = 0;
}

int f( bool b ) {
   return myvar;
}

int f( const string &  s  ) {
   return myvar;
}

To make the functions members of a class:

// a.h
class A {
   public:
    static int f( bool b ) {
       return myvar;
    }

    static int f( const string &  s  ) {
       return myvar;
    }
  private:
     static int myvar;
};

// a.cpp
int A::myvar = 0;   

// main.cpp

#include <iostream>
#include <a.h>
int main() {
    std::cout << A::f(false) << A::f( string("foobar") ) << std::endl;   
}
anon
Unfortunately, since this function is continuously used, it is not being encapsulated within a class.
BSchlinker
@BSchlinker Whether or not it is continuously used should not prevent you using a class. But there are other alternatives, as my answer suggests.
anon
Is it possible to put the functions from a class into a namespace, so they are essentially accessible from everywhere?
BSchlinker
@BSchlinker There is no need to use a namespace to do this - I've added some code to illustrate.
anon
A: 

The answer is no. There is no reason it should be, since after all we are talking about 2 functions.

Since it's already been demonstrated, I'd like to address the very core of the matter: static.

static introduces global state, and global state is evil. It leads to subtle bugs, difficulties to test properly (since a test affects the ones executed after it) and don't even think about going multithreaded there...

Therefore I would really encourages you to avoid the static entirely. You would then have 2 solutions:

  • Make a class with the two overloads as methods, and store state (not static, please)
  • Pass the bool as parameter to the methods, out-parameter for the bool overload and in-parameter for the string overload

Pick up whichever is easier to achieve.

Matthieu M.