He claimed you can just do n++ instead of n = n->next. I disagree.
With your List class that's true. But C++ allows you to overload operators to do different things. You can overload the ++ operator to do n = n->next. That's what the list::iterator does:
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> values;
for (int x = 0; x < 10; ++x) values.push_back(x);
for (list<int>::iterator x = values.begin();
x != values.end();
++x)
{
cout << *x << '\n';
}
} list::iterator is a class that overloads the ++ operator and other things to match certain rules about how the iterator type should work. In a way list::iterator corresponds to your Node class.
[edit]
This is a quick example to show what I mean. It uses a wrapper around Node to implement an iterator. It's not fancy or 100%, but it shows the concept a little better than the first example.
#include <iostream>
#include <cstddef>
using namespace std;
class Node {
public:
int data;
Node * next;
};
class Iterator {
friend class List;
Node *_node;
public:
Iterator(Node *node=NULL): _node(node) {}
int& operator*() { return _node->data; }
Iterator& operator++()
{
_node = _node->next;
return *this;
}
friend bool operator==(const Iterator& lhs, const Iterator& rhs)
{
return lhs._node == rhs._node;
}
friend bool operator!=(const Iterator& lhs, const Iterator& rhs)
{
return !(lhs._node == rhs._node);
}
};
class List {
public:
List();
void append(int i);
void print();
void test();
private:
Iterator front;
Iterator back;
};
List::List() {
front = back = NULL;
}
void List::append(int i) {
Node *n = new Node;
n->data = i;
n->next = NULL;
if (front == Iterator()) {
front._node = back._node = n;
} else {
back._node->next = n;
back._node = n;
}
}
void List::print() {
cout << "printing" << endl;
for (Iterator n = front; n != Iterator(); ++n) {
cout << *n << endl;
}
}
int main() {
List l;
l.append(1);
l.append(2);
l.append(3);
l.print();
return 0;
}