我有一堂课:
template <typename T>
class List {
private:
struct pointNode {
T data;
pointNode* next;
pointNode* prev;
pointNode() :data(0), next(nullptr), prev(nullptr) {}
pointNode(T n_data) : data(n_data), next(nullptr), prev(nullptr) {}
const T& getValue() {
return this->data;
}
};
pointNode* head;
pointNode* tail;
public:
class Iterator {
//friend class List;
using Iterator_type = List<T>::pointNode;
public:
Iterator(Iterator_type* rNode) {
current_node = rNode;
}
bool operator !=(const Iterator& pNode) {
return this->current_node != pNode.current_node;
}
T const get_value() {
return this->current_node->data;
}
private:
Iterator_type* current_node;
};
List() : head(nullptr), tail(nullptr) {}
inline void InsertFront(T&& val);
inline void InsertBack(T&& val);
inline bool is_empty();
inline void Insert_after_v(T&&searchVal,T&&val);
inline void Insert_after_p(int pos, T&& val);
};
Run Code Online (Sandbox Code Playgroud)
我正在尝试编写一个在给定后插入元素的函数:
template<typename T>
inline void List<T>::Insert_after(Iterator pos, T&& val)
{
pointNode* new_node = new pointNode(std::move(val), ... );
}
Run Code Online (Sandbox Code Playgroud)
作为构造函数pointNode的2个参数,我需要获取迭代器的值
private:
Iterator_type* current_node;
Run Code Online (Sandbox Code Playgroud)
我认为这是正确的做法?交个朋友:
struct pointNode {
friend class Iterator
Run Code Online (Sandbox Code Playgroud)
但是我经常看到那个朋友不是一个特别好的风格。
class Iterator
{
public:
Iterator_type* get_current_node() const {
return current_node;
}
Run Code Online (Sandbox Code Playgroud)
或写这样的东西:
class Iterator
{
public:
Iterator_type* get_node() const {
return current_node;
}
Run Code Online (Sandbox Code Playgroud)
但是在我看来,让用户获得私人数据并不是很好。那么也许谁知道在这种情况下如何做正确的事情?我将不胜感激
| 归档时间: |
|
| 查看次数: |
52 次 |
| 最近记录: |