Method as a pointer ?

Hello everyone,
after searching Google for two hours I'm still totally confused about the following example of an handout.

What I don't understand is the part
class element *get_item();
class node *get_next();

1. Are get_item and get_next member functions?
2. If yes why are they not decelerated by node::get_item();
3. What is there type, or are they really pointers?

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
class element{
public:
    string name;
};

class node{
private:
    class element *item;
    class node *next;
public:
    node();
    node(class node *n);
    ~node();
    // member functions
    class element *get_item();
    class node *get_next();
    int set_next(class node *n);
};

...


// member functions definition
class element *node::get_item(){
    return item;
}

class node *node::get_next(){
    return next;
}

1. yes.
2. You only need node:: when you are referring to the functions outside the class definition.
3. get_item() returns a pointer to an element and get_next() returns a pointer to a node. It's not necessary to have the class keyword in front of the return type like they do in the code.
Ahh ok thanks a lot, the class thing really confused me.
So get_item(): returns a pointer to an element object, which is a pointer
element *item;?
Are get_item and get_next member functions?
Yes, as they modify or use the object status.
If that were not the case, it would make no sense that you need an object in order to call them.

That said, I would consider bool empty(const container &c); as a member function of `container'.


However, get_next() and set_next() are breaking the encapsulation.

So get_item(): returns a pointer to an element object, which is a pointer element *item;?
An node wraps around an element.
It's one of its responsibilities to provide access to the wrapped element. That's what get_item() does.
Great!!!
Thanks a lot, you really helped me to understand the script.

cheers
Topic archived. No new replies allowed.