关于 C++ STL list 之迭代器
答案:1 悬赏:60
解决时间 2021-02-16 00:29
- 提问者网友:话酸浅沫
- 2021-02-15 21:21
//list 迭代器基类[部分代码]
struct _List_iterator_base {
_List_node_base* _M_node;
bool operator==(const _List_iterator_base& __x) const {
return _M_node == __x._M_node;
}
bool operator!=(const _List_iterator_base& __x) const {
return _M_node != __x._M_node;
}
};
////list 迭代器类[部分代码]
template<class _Tp, class _Ref, class _Ptr>
struct _List_iterator : public _List_iterator_base {
reference operator*() const { return ((_Node*) _M_node)->_M_data; }
pointer operator->() const { return &(operator*()); }
_Self& operator++() {
this->_M_incr();
return *this;
}
_Self operator++(int) {
_Self __tmp = *this;
this->_M_incr();
return __tmp;
}
_Self& operator--() {
this->_M_decr();
return *this;
}
_Self operator--(int) {
_Self __tmp = *this;
this->_M_decr();
return __tmp;
}
};
问:
为什么 比较操作符 == != 重载 的实现 放在 基类 _List_iterator_base
而不是和 -> * ++ -- 一样 放在 _List_iterator 当中
这是出于什么考虑?
谢谢!!!
最佳答案
- 二级知识专家网友:24K纯糖
- 2021-02-15 21:42
struct _List_iterator_base {
_List_node_base* _M_node;
bool operator==(const _List_iterator_base& __x) const {
return _M_node == __x._M_node;
}
bool operator!=(const _List_iterator_base& __x) const {
return _M_node != __x._M_node;
}
};
////list 迭代器类[部分代码]
template<class _Tp, class _Ref, class _Ptr>
struct _List_iterator : public _List_iterator_base {
reference operator*() const { return ((_Node*) _M_node)->_M_data; }
pointer operator->() const { return &(operator*()); }
_Self& operator++() {
this->_M_incr();
return *this;
}
_Self operator++(int) {
_Self __tmp = *this;
this->_M_incr();
return __tmp;
}
_Self& operator--() {
this->_M_decr();
return *this;
}
_Self operator--(int) {
_Self __tmp = *this;
this->_M_decr();
return __tmp;
}
};
_List_node_base* _M_node;
bool operator==(const _List_iterator_base& __x) const {
return _M_node == __x._M_node;
}
bool operator!=(const _List_iterator_base& __x) const {
return _M_node != __x._M_node;
}
};
////list 迭代器类[部分代码]
template<class _Tp, class _Ref, class _Ptr>
struct _List_iterator : public _List_iterator_base {
reference operator*() const { return ((_Node*) _M_node)->_M_data; }
pointer operator->() const { return &(operator*()); }
_Self& operator++() {
this->_M_incr();
return *this;
}
_Self operator++(int) {
_Self __tmp = *this;
this->_M_incr();
return __tmp;
}
_Self& operator--() {
this->_M_decr();
return *this;
}
_Self operator--(int) {
_Self __tmp = *this;
this->_M_decr();
return __tmp;
}
};
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯
• 手机登qq时,显示手机磁盘不足,清理后重新登 |
• 刺客的套装怎么选啊? |