c++ - 由于私有(private)继承而无法访问迭代器

标签 c++ stl std inner-classes

我创建了一个新类,它通过私有(private)继承组成 std::deque,即,

class B : private std::deque<A>
{ ... };

在我的源代码中,我尝试使用 B 的迭代器,即,

B::iterator it

编译错误是

error C2247: 'std::deque<_Ty>::iterator' not accessible because 'B' uses 'private' to inherit from 'std::deque<_Ty>'

那么问题是,我怎样才能使迭代器可访问?

最佳答案

你必须提升这个迭代器类。

在公共(public)部分使用using关键字。

class B : private std::deque<A>
{ ... 
 public:
   using std::deque<A>::iterator;    
};

对于其他类型以及来自实现基类的其他函数也是如此。

关于c++ - 由于私有(private)继承而无法访问迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12533541/

相关文章:

c++ - C++ 标准是否允许自定义类型的 std::to_string 特化?

c++ - 调试 C/C++ 语言不命中断点

c++ - 当我复制 pthread_rwlock_t 时会发生什么?

c++ - 如何从其序列号中获取消息请求?

c++ - 为哈希表初始化 STL 列表的动态数组(单独链接)

c++ - 当我尝试清理STL vector 时发生未处理的异常

c++ - 如何使用 Gmock 模拟不带运算符 == 的函数参数

c++ - 在 g++ 中编译期间出现 max_element STL 错误

c++ - 为什么 C++ Compare 不需要整数作为其返回值?

c++ - 为什么没有 std::stou?