I’m attempting to create my own double linked list for learning experience. My book showed the node struct below and I was wondering is that equivalent to my Node class I created? Is that function in the struct just a type of constructor assigning values to each data type in the struct?
//===== Struct =====
struct Node
{
Node *next;
Node *prev;
std::string val;
Node(const std::string &value, Node *nextVal = NULL, Node *prevVal = NULL) :
val(value), next(nextVal), prev(prevVal) {}
};
//===== Class ====
class Node
{
public:
Node(std::string value = "", Node *pVal = NULL, Node *nVal = NULL);
virtual ~Node(void);
protected:
Node *next;
Node *prev;
std::string val;
};
Node(std::string value = "", Node *pVal = NULL, Node *nVal = NULL)
{
next = nVal;
prev = pVal;
val = value;
}
Yes – that’s exactly what it is.
Here’s a page with an example of a struct constructor.
http://www.yolinux.com/TUTORIALS/LinuxTutorialC++Structures.html