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>;
	};

};


From my knowledge one can use -> when a pointer tries to call access class functions members
It does not matter if they are functions or data.

current = current -> next; is same as current = (*current).next;: t assigns a Node pointer current value of field next of object current is pointing to
Hello MiiNiPaa,

I appreciate your help.
I am sorry but I can't understand what you mean by "t assigns a Node pointer current value of field next of object current is pointing to."

Could you further help me on answering four questions in the code below? Q3 is a redundant question, I think, but I still don't get the concept :(

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?
};
Last edited on
Q1. Line 7: You're trying to initialize a member variable in the class declaration. You can't do that unless it's a static const.

Line 8: This is an executable statement. It belongs in a function, not in a class declaration.

Q2. Ditto.

Q3. A pointer to a pointer is simply the address of a variable which also contains an address of something.

Q4. int * z is a pointer to an int. foo * x is a pointer to an object of type foo.
Last edited on
Q1: because you cannot write code in class definition. Only member-declarations (with in-class initializers in C++11) are allowed. YOu can write
1
2
int k = 3;
int* z = &k;

Q2: same reason as Q1.

Q3: y->k means (*y).k. This is how operator -> is defined in ISO C++ Standard.
1
2
3
4
5
6
7
8
9
10
struct foo {
    int x;
};
//...
foo  bar;
foo* baz = &bar;
//Following lines code have same meaning:
bar.x = 5;
baz->x = 5;
(*baz).x = 5;


Q4: z is a pointer to int, and x is a pointer to foo
Last edited on
Thank you so much for explanation.
I understand how an arrow operator is used when a pointer bar accesses an integer member x.
Could you explain a difference in following example codes?
Thank you.

1
2
3
4
5
foo z;
foo *x = &z;
foo k;
foo *y = &k;
x = y;


1
2
3
4
5
foo z;
foo *x = &z;
foo k;
foo *y = &k;
x = x->y;



Last edited on
If first case value of y (address of k) is assigned to x, in result x and y are both pointing to k

In second case x is assigned value a member of foo y contains. It will work if foo has a member of type foo* named y:
1
2
3
struct foo {
    foo* y;
}
Visualization: http://puu.sh/ko0uF/8f8876c847.png
Thank you so much MiiNiPaa. Visualization helps me a lot to see what is going on:)

In line 2, x was defined to be a pointer.
In line 5, x->y is equivalent to a value (something) foo member y contains, and so
x = x-> y means x = something. Can something be a data type integer? If it is,
does that change the data type of x from a pointer to an integer?

Also, how can possibly a member of foo y, which is a pointer, contain value?
I thought only object (like foo z;) can hold attributes of a class. What syntax should I use
to let a pointer member hold attributes of a class?

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

struct foo{
	foo *y;
	int k;
};

int main(){
	y->k = 5; //shows an error message. How can a pointer member y hold an integer value 5 in it?

	foo z;
	foo *x = &z;
	x = x->y;
	
	
	system("pause");
	return 0;
}
does that change the data type of x from a pointer to an integer?
C++ is statically typed. When something is declared, you need to tell what type is it, and it stays that type forever.

y->k = 5; //shows an error message. y is a member of foo object, you need an objec or pointer to it to access y.
Example with better naming:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
struct foo
{
    foo* other;
    int value;
}

int main()
{
    foo object1;
    foo object2;
    object2.other = &object1; //Make other in object2 refer to object1

    foo* pointer = &object2; //Make pointer point to object2
    //Retrieve object referred by pointer (object2), access its field other
    //Access object referred by that pointer (object1) and access field value
    //Set it to 5
    pointer->other->value = 5;
    
    //Now object1 value is 5
}

how can possibly a member of foo y, which is a pointer, contain value?
Pointer points to object, which can contain anything.

Look at it this way:
you take the slip of paper with address (pointer), find a house written here (object pointer refers to) and steal their garden gnome (access that object member). Garden gnome isn't been contained in slip of paper, but this slip of paper allows us to find it.
I truly appreciate you for your code example with the meaning of them. Now I understand the meaning of arrow operators in your example.

However, when I created a different type of a code example where struct foo has two pointers members, other problems arose. Whenever arrow() is accessed by Object, it shows an error message EXC_BAD_ACCESS, and really don't know the meaning of the code
other1 = other1->other2. Can you help me out again?
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

#include <iostream>
using namespace std;

struct foo{
    int x;
    foo *other1;
    foo *other2;
    void arrow(){
        other1 = other1->other2;
    }
    
};

int main(){
    foo Object;
    Object.arrow();
    
    cout << Object.other1 <<endl;
    cout << Object.other2<<endl;
   
    return 0;
}
Last edited on
Line 16: You instantiate Object. other1 and other2 are uninitialized (garbage).
Line 17: You call Object's arrow method.
Line 10: You attempt to reference another object of type foo using the pointer other1, but other1 is uninitialized. This is what is causing EXC_BAD_ACCESS.
Hi AbstractionAnon,

thank you for your reply. How can I initialize a pointer other1? Can I do that in the class or in the body of the function main or either one? If other1 was already initialized, is the body of arrow() free of errors?

Also, what does it mean by "reference another object of type foo using the pointer other1?"
I am uncertain about the word reference used in your statement.

Sorry for asking too many questions.


How can I initialize a pointer other1?

Set it to something. Consider creating a constructor for your class (struct). Constructors are the logical place to initialize something.

If other1 was already initialized, is the body of arrow() free of errors?

Syntactically yes. Logically, no. other1 supposedly points to another object of type foo, but you have only a single object of type foo in your program.



Topic archived. No new replies allowed.