Syntax for Pointer

Hello,
I am having trouble with recognizing the syntax related to pointers and arrow operator.
In the body of the function const_iterator& operator++(), the code
current = current -> next;
confuses me because of unfamiliar usage of an arrow operator in that code. From my knowledge one can use -> when a pointer tries to call class functions. Both members (current and next) are pointers, and I don't know how an arrow operator can be used with two pointers like that.
My second question is that the meaning of the code
++(*this);
in the body of function const_iterator operator++(int).

Thank you:)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#ifndef LIST_H // // multiple inclusion guard
#define LIST_H

template <typename Object>
class List
{
private:
	struct Node // private member structure (could be class instead)
	{
		Object data;
		Node* prev; 
		Node* next;
		Node(const Object& d = Object(), Node* p = NULL, Node* n = NULL) // constructor
			: data(d), prev(p), next(n) {}
		Node(Object&& d, Node* p = NULL, Node* n = NULL) // move constructor (C++11)
			: data(move(d)), prev(p), next(n) {}
	};

public:
	class const_iterator // **********************************************
	{
	public:
		const_iterator() : current(NULL) {} // default constructor

		const Object& operator*() const // const de-referencing
		{
			return retrieve();
		}

		const_iterator& operator++() // pre-increment
		{
			current = current->next;  //?
			return *this;
		}

		const_iterator operator++(int) // post-increment
		{
			const_iterator old = *this;
			++(*this); //?
			return old;
		}

		const_iterator& operator--() // pre-decrement
		{
			current = current->prev;
			return *this;
		}

		const_iterator operator--(int) // post-decrement
		{
			const_iterator old = *this;
			--(*this);
			return old;
		}

		bool operator== (const const_iterator& rhs) const
		{
			return current == rhs.current;
		}

		bool operator != (const const_iterator& rhs) const
		{
			return !(*this == rhs);
		}

	protected:
		Node* current;

		Object& retrieve() const
		{
			return current->data;
		}

		const_iterator(Node* p) : current(p) {} // constructor

		friend class List<Object>;
	};

};



Last edited on
The -> operator is simply a short hand for the dereference and dot operator.

1
2
3
4
Foo* foo = getNewFoo();
//These are equivalent
foo->bar;
(*foo).bar;
Hi RB,

Thank you for your explanation.

Could you help me to answer four questions in the code below?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

class foo{
public:
	int *z;
	int k = 3;
	z = &k; // Q1. Why does this code show an error message?

	foo *x; 
	foo *y;
	y = y->x; // Q2. Why does this show an error message? Q3. What does y->k (a pointer -> a pointer) mean?

	//Q4. What is the difference between int *z and foo *x?
};
Q1. Look at the types and remember that a pointer is its own type. Does this look like it will work?
Q2. What does y point to? What is y->x?
Q3. I already explained that operator.
Q4. Look at the types.
none of this is supposed to work since it is in a class declaration !
Change the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

class foo{
public:
	int *z;
	int k = 3;
	int *z = &k; // Q1. Why does this code show an error message?

	foo *x; 
	foo *y;
	foo *y = y->x; // Q2. Why does this show an error message? Q3. What does y->k (a pointer -> a pointer) mean?

	//Q4. What is the difference between int *z and foo *x?
};
It compiles (Note: only from c++11 on), but all pointer (except z) just contain garbage.

Q3. Another pointer (wherever x points to).
Q4. Like already stated: different types.
Topic archived. No new replies allowed.