【发布时间】:2017-12-07 18:18:52
【问题描述】:
给定
struct foo {
struct node {
int data;
node* next, prev;
};
friend std::ostream& operator<< (std::ostream& os, const foo::node& n) {
os << n.data;
return os;
}
// friend node* operator++ (list::node* n) { return n->next; }
};
我需要重载仅作用于foo::node* 的++。我在哪里以及如何定义该运算符?我上面注释掉的行表明我正在尝试做的事情。谢谢。
一个示例用法是:
for (list::node* it = f.begin(); it != f.end(); ++it)
std::cout << *it << ' ';
其中f 是foo 对象,begin() 和end() 函数返回一些foo::node 指针。
【问题讨论】:
-
如果只涉及原始类型,则不能定义运算符重载。
-
您需要为此创建一个迭代器类。你不能只使用指针。
标签: c++ pointers operator-overloading