Say I have a linked list node class
class node {
private:
node *next_node;
public:
node *next() const;
};
node *node::next() const {
return next_node;
}
Does next() return a node **next_node or node *next_node. Also what is the significance of either in implementing the list class functions (ie. insert, remove, find)?
The reason I would think it returns a **next_node is because next_node is already a pointer and returning it in a function as a pointer would make it a pointer to a pointer. I read in other questions such as: Linked list head double pointer passing that double pointers also work in list operations so I was a bit confused.
The
node::next()function as implemented is returning a copy of thenext_nodemember variable. Bothnext_nodeand this copy point to the samenodeinstance but they are otherwise independent of each other.Here is some rather bad ASCII art that tries to demonstrate their relationship
next_node ----> [node instance] ^ the copy ------/