开发者

Is there a way to have a single static variable for a template class (for all types) without breaking encapsulation

I need a way to have a single static variable for all kinds of types of my template class

template <class T> class Foo { static Bar foobar;};

Well, the line above will generate a Bar object named foobar for every type T, but this is not what i want, i basically want a way to declare a variable of type Bar, so every object of type Foo has access to the same foobar variable, independent of T.

I tried to use another class to store the private stuff, but that do开发者_JAVA技巧esnt work, because the standard does not allow stuff 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 information hiding concept (of 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;

Ofcourse you can additionally use a detail namespace (thats what i am currently doing), but is there another way which really prohibits users from messing around with your private static variables ?

Additonally this solution will get quite messy when you have to declare lots of static methods in a similar fashion, because you will most likely have to extensivly use the friend keyword like friend RetType Foo_detail::StaticFunc(ArgT1, ArgT2).

And the users wont have a nice interface since they cant use those functions like they are used to Foo<T>::someFunc() but instead they will have to call something like Foo_static::someFunc() (if you use the namespace Foo_static for public static functions).

So is there any other solution which does not break encapsulation, and/or does not introduce lots of syntax overhead ?

EDIT: based on all your anwsers, i tried following, and it works as intended:

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;
};

this solution has the benefit, that users can not inherit from FooBase.


Perhaps inherit the static member?

class OneBarForAll
{
protected:
  static Bar foobar;
};

template <class T>
class Foo : public OneBarForAll
{

};

Lots of Foo<T>'s will be made, but only one OneBarForAll.

One potential problem with this is that there's nothing stopping other users of the code from inheriting from OneBarForAll and modifying foobar anyway.

Ideally you do want the template friend, as that best describes the access requirements of your design, but C++ does not currently allow that.


The syntax for

template <class T> friend class Foo<T>;

is

template <class T> friend class Foo;

(which means that every instantiation of Foo is a friend of the class you define it in)

So perhaps you can go with the solution you ruled out before.


you could do:

struct Base {
  static Foo foo;
};
//init foo here

template<typename T>
struct Derived : Base {
...
};
...
Derived<Bar>::foo;

It works in g++


Why not inherit from a non-template base class?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜