长话短说,问题是:
template<class T>
struct alloc
{
template<class U>
alloc(alloc<U> const &other) : foo(other.foo) { } // ERROR: other.foo is private
template<class U> struct rebind { typedef alloc<U> other; };
private:
pool<T> *foo; // do I HAVE to expose this?
};
是公开公开私有(private)字段的唯一解决方案吗?
您应该如何实际创建转换构造函数?
最佳答案
在模板复制构造函数中,alloc<T>
和alloc<U>
是不同的类型,意味着您无法访问 alloc<U>
的私有(private)成员在这里。
您可以制作alloc<U>
friend :
template<class T>
struct alloc
{
... ...
template <typename U>
friend struct alloc;
alloc(alloc<U> const &other) : foo(other.foo) {} // possible to access other.foo now
};
关于c++ - 我应该如何使我的分配器可重新绑定(bind)?我可以在保持其字段私有(private)的同时做到这一点吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38045672/