模板组合和友谊传递性
Template composition and friendship transitivity
我有包含私有数据的容器和访问私有数据的朋友 class:
template<class T>
class Container
{
friend typename T::MyAccessor;
vector<T> _data;
};
template<class T>
class Accessor
{
public:
void doSomething(Container<T> *c)
{
cout << c->_data.size() << endl;
}
};
template<class T, template<typename> class CustomAccessor>
struct MyBase
{
typedef Container<T> MyContainer;
typedef CustomAccessor<T> MyAccessor;
};
现在我想这样组合访问器:
template<
template<typename> class First
,template<typename> class Second
,class T
>
class Composite
{
public:
typedef First<T> MyFirst;
typedef Second<T> MySecond;
void doSomething(Container<T> *c)
{
MyFirst a;
a.doSomething(c);
MySecond b;
b.doSomething(c);
}
};
template<class T>
class DoubleAccessor : public Composite<Accessor, Accessor, T> {};
但是友谊是不可传递的,组合访问者不能访问容器私有数据。
有没有办法在不向所有人公开容器私有数据的情况下解决这个问题?
如果您只是将 class Accessor
声明为 class Container
的 friend
怎么办:
template<class T>
class Container {
template<class T1> friend class Accessor;
std::vector<T> _data;
};
template<class T>
class Accessor {
public:
void doSomething(Container<T> *c) {
std::cout << c->_data.size() << std::endl;
}
};
template<class T, template<typename> class CustomAccessor = Accessor>
struct MyBase {
typedef Container<T> MyContainer;
typedef CustomAccessor<T> MyAccessor;
};
template<class T, template<typename> class First = Accessor, template<typename> class Second = Accessor>
class Composite {
public:
typedef First<T> MyFirst;
typedef Second<T> MySecond;
void doSomething(Container<T> *c) {
MyFirst a;
a.doSomething(c);
MySecond b;
b.doSomething(c);
}
};
我有包含私有数据的容器和访问私有数据的朋友 class:
template<class T>
class Container
{
friend typename T::MyAccessor;
vector<T> _data;
};
template<class T>
class Accessor
{
public:
void doSomething(Container<T> *c)
{
cout << c->_data.size() << endl;
}
};
template<class T, template<typename> class CustomAccessor>
struct MyBase
{
typedef Container<T> MyContainer;
typedef CustomAccessor<T> MyAccessor;
};
现在我想这样组合访问器:
template<
template<typename> class First
,template<typename> class Second
,class T
>
class Composite
{
public:
typedef First<T> MyFirst;
typedef Second<T> MySecond;
void doSomething(Container<T> *c)
{
MyFirst a;
a.doSomething(c);
MySecond b;
b.doSomething(c);
}
};
template<class T>
class DoubleAccessor : public Composite<Accessor, Accessor, T> {};
但是友谊是不可传递的,组合访问者不能访问容器私有数据。 有没有办法在不向所有人公开容器私有数据的情况下解决这个问题?
如果您只是将 class Accessor
声明为 class Container
的 friend
怎么办:
template<class T>
class Container {
template<class T1> friend class Accessor;
std::vector<T> _data;
};
template<class T>
class Accessor {
public:
void doSomething(Container<T> *c) {
std::cout << c->_data.size() << std::endl;
}
};
template<class T, template<typename> class CustomAccessor = Accessor>
struct MyBase {
typedef Container<T> MyContainer;
typedef CustomAccessor<T> MyAccessor;
};
template<class T, template<typename> class First = Accessor, template<typename> class Second = Accessor>
class Composite {
public:
typedef First<T> MyFirst;
typedef Second<T> MySecond;
void doSomething(Container<T> *c) {
MyFirst a;
a.doSomething(c);
MySecond b;
b.doSomething(c);
}
};