【问题标题】:What's the relation between a class SUP and a class SUB which is defined in SUP in c++?类 SUP 和 C++ 中 SUP 中定义的类 SUB 之间的关系是什么?
【发布时间】:2011-04-08 14:09:23
【问题描述】:

我想写一个叫my_list的容器:

template<typename T>
class my_list {
    public:
        // ...

        class iterator {
            private:
                node* it;
        }
    private:
        struct node {
            T item;
            node* next;
        }

        node* head;
        node* end;
        int count;
}

但是,iterator 类不能使用 my_list 类中的私有数据成员。我查阅了一些 c++ 书籍,但没有找到任何相关内容。

【问题讨论】:

  • 除非这是家庭作业或学习,否则首选std::list。它已经编写、调试和优化,并且有大量文档。

标签: c++ class iterator


【解决方案1】:

继续阅读tutorial 中朋友类的用法。迭代器是正确使用该语言特性的一个非常典型的例子。

顺便说一句,您不使用 STL 数据结构而不是滚动您自己的数据结构有什么原因吗?见list

【讨论】:

  • 你只需要知道节点的结构,而不需要成为列表的朋友。实际上,应该编写迭代器,这样我们就不需要使用朋友关系(在大多数标准容器中,这是可能的)
猜你喜欢
  • 2011-10-10
  • 2020-10-22
  • 2023-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-14
  • 1970-01-01
  • 2020-09-19
相关资源
最近更新 更多