May 23, 2018 at 7:53am UTC
what does "this" keyword means and also the line Node* headnode = new Node() mean.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
class Node
{
public :
void set(int object){
this ->object=object;
}
private :
int data;
Node * headnode;
};
Node * headnode = new Node();
Last edited on May 23, 2018 at 7:54am UTC
May 23, 2018 at 8:03am UTC
this
is a pointer. In every instance of a class, it points to that instance of the class; points to itself.
Node* headnode
This means "create a new Node pointer, named headnode"
new Node();
This means "create a new Node object, on the memory heap, and give me a pointer to it".
=
means assign the value of the thing on the right to the thing on the left
Node * headnode = new Node();
So this creates a new Node object on the heap, and headnode is a pointer to that new Node object.
May 23, 2018 at 8:34am UTC
Last edited on May 23, 2018 at 8:37am UTC
May 24, 2018 at 3:51am UTC
Sir i have another question when an object is initialized than if we do not delete its address by using delete operator and make a new object by writing function than this object exists in heap memory or it is overrides and a new object takes place.
May 24, 2018 at 8:38am UTC
If you create an object using new, and you never delete that object, it will still exist.