contructor and destructor

Hello every body!!

please, can you tell me what that code does?

it's a linked list
thank you very much
Node(const Type& inValue, Node* inPrev, Node* inNext): mValue(inValue), mPrev(inPrev), mNext(inNext)
{
if(mPrev) mPrev->mNext = this;
if(mNext) mNext->mPrev = this;
}

//! Unlink this node.
~Node(void)
{
if(mPrev) mPrev->mNext = mNext;
if(mNext) mNext->mPrev = mPrev;
mPrev = mNext = 0;
}

O_O please for God sake, use CODE !!
like this
1
2
3
4
5
6
7
8
9
10
11
12
Node(const Type& inValue, Node* inPrev, Node* inNext): mValue(inValue), mPrev(inPrev), mNext(inNext)
{
if(mPrev) mPrev->mNext = this;
if(mNext) mNext->mPrev = this;
}

//! Unlink this node.
~Node(void)
{
if(mPrev) mPrev->mNext = mNext;
if(mNext) mNext->mPrev = mPrev;
mPrev = mNext = 0;
This is specifically a doubly linked list. Each node has a pointer to the next one, and a pointer to the last one. The constructor will do the following, in more C-style syntax:
1
2
3
4
5
6
7
8
9
Node Node(inValue,inNext,inPrev){
    Node Node;
    Node.mValue=inValue;//point to the data
    Node.mNext=inNext;//point to the next node
    Node.mPrev=inPrev;//point to the previous node
    if(Node.mPrev)Node.mPrev->mNext=&Node;//make the previous node point to us as its next node
    if(Node.mNext)Node.mNext->mPrev=&Node;//make the next node point to us as its previous node
    return Node;//now give whoever called us the new node.
}

That's basically what it does. Make sure you learn the weird f(param): var(param) syntax, it's called an initializer list. Some people use it a lot to save typing a lot of constructor calls in a constructor.
The destructor does this:
1
2
3
4
5
6
void ~Node(Node){
    if(Node.mPrev)Node.mPrev->mNext = Node.mNext;//make the previous node point to the next node instead of us.
    if(Node.mNext)Node.mNext->mPrev = Node.mPrev;//make the next node point to the previous one.
    Node.mNext=Node.mPrev=0;//the node should no longer point to either the next or previous ones.
    //now the node will be deallocated, and we can rest assured that no-one has a pointer to it. (or can we?)
}


That's basically what it does. Make sure you learn the weird f(param): var(param) syntax, it's called an initializer list. Some people use it a lot to save typing a lot of constructor calls in a constructor.


Actually it goes beyond save typing of code. If you read Scott Meyers Effective C++, the difference lies in how the C++ run-time initialize those members. They are not the same from that point of view. From source code point of view, you feel it is saving keystrokes which I used to think like that too in the past.
thank you very much!!
Topic archived. No new replies allowed.