I need a way to have one static variable for all type types of my template class
template <class T> class Foo { static Bar foobar;};
Well, the line above will create a Bar object named foobar for each type T, but that’s not what I want, I basically want a way to declare a variable of type Bar, so every type object Foohas access to the same variable foobar, regardless T.
I tried to use another class to store personal items, but this does not work, because the standard does not allow things like template <class T> friend class Foo<T>;
So, the obvious solution (shown below) is to have a global variable Bar foobar, but this obviously violates the concept of information hiding (proper encapsulation):
Bar Foo_DO_NOT_TOUCH_THIS_PLEASE_foobar;
template <class T> class Foo { static Bar& foobar;};
template <class T> Bar& Foo<T>::foobar=Foo_DO_NOT_TOUCH_THIS_PLEASE_foobar;
Of course, you can additionally use the part namespace (this is what I'm doing now), but is there another way that really prevents users from communicating with their private static variables?
Also, this solution will be rather messy when you have to declare many static methods in the same way, because you will most likely have to extensively use the friend keyword, for example friend RetType Foo_detail::StaticFunc(ArgT1, ArgT2).
And users will not have a nice interface, because they cannot use the functions that are used for Foo<T>::someFunc(), but instead they will have to call something like Foo_static::someFunc()(if you use the namespace Foo_staticfor public static functions).
, , , / ?
EDIT:
anwsers, , :
typedef int Bar;
template <class T> class Foo;
class FooBase
{
static Bar foobar;
public:
template <class T> friend class Foo;
};
Bar FooBase::foobar;
template <class T> class Foo : public FooBase
{
public:
using FooBase::foobar;
};
, FooBase.