I have been relying on the misconception that static variables defined in member functions were limited to the specific class instance.
To illustrate my misconception:
#include <iostream>
#include <string>
struct Simple {
template<typename T>
T & Test(const T & Value) {
static T Storage = Value;
return Storage;
}
};
int main() {
Simple A;
Simple B;
std::string Foo = A.Test(std::string("Foo"));
std::string Bar = B.Test(std::string("Bar"));
std::cout << Foo << ' ' << Bar << std::endl;
}
The behavior I expected would have resulted in the output of
Foo Bar
Is there a simple alternative that would result in the behavior I expected?
Edit
A cutdown version of the class that has the issue:
class SignalManager {
private:
template<typename T> struct FunctionPointer { typedef boost::function1<void, const T &> type; };
template<typename T> struct Array { typedef std::vector<typename FunctionPointer<T>::type> type; };
template<typename T>
typename Array<T>::type & GetArray() {
static typename Array<T>::type Array;
return Array;
}
public:
template<typename T, typename M>
void Broadcast(const M & Value) {
typename Array<T>::type::iterator Iterator;
for(Iterator = GetArray<T>().begin(); Iterator != GetArray<T>().end(); ++Iterator) {
(*Iterator)(Value);
}
}
template<typename T, typename F>
void Connect(const F & Function) {
GetArray<T>().push_back(Function);
}
};
If I understand your confusion, the alternative is member variables…