我该如何使我的分配器可重新绑定?我能做到这一点,同时保持其字段的私有
How am I supposed to make my allocator rebindable? Can I do it while keeping its fields private?
长话短说,问题是这样的:
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?
};
是公开私有字段的唯一解决方案吗?
你应该如何创建转换构造函数呢?
在模板复制构造函数中,alloc<T>
和alloc<U>
是不同类型的,这意味着你不能在这里访问alloc<U>
的私有成员。
你可以让alloc<U>
成为朋友:
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
};
我自己的猜测是这是不可能的,您应该使用转换操作符:
template<class U>
operator alloc<U>() const { return alloc<U>(this->foo); }
但我希望有更好的答案…
相关文章:
- 将结构字段的类型展开为可变模板参数
- 将位字段导出到数组
- 为了方便起见,我应该避免公开私有字段变量吗
- 当字段可以为null时,如何使用C++接口在Avro中写入数据
- 在java中读取c++字节的位字段
- 链接器找不到在虚拟类 c++ 中访问的静态字段的符号
- 私有字段对象与私有继承?
- 声明没有默认构造函数的字段
- C++内存模型和位字段的最大序列
- 声明为无效的变量或字段'...' Ardunio 编译器上的错误
- 如何在QByteArray中放置和检索位字段而不会感到痛苦?
- C++ win32 如何使密码字段可选并启用复制和粘贴?
- 如何通过UDP接收QByteArray并将其解析为位字段结构?
- 仅匹配集合中的某些字段
- 结构字段名称与 GDB 中的 STL 数组冲突
- 如何使用位字段将数据从二进制文件复制到结构中?
- 联盟中的位字段 - 这是多么便携
- 这两个字段/方法的区别是什么?
- 为什么这可以让我获得一个指向常量对象字段的非常量指针
- 我该如何使我的分配器可重新绑定?我能做到这一点,同时保持其字段的私有