0

I found this C++ linked list implementation in Geeks for Geeks. For the code Click here

Here from line no. 16->22 it is written

Node *head = NULL;
Node *second = NULL;
Node *last = NULL;    
head = new Node();
second = new Node();
last = new Node();

I understand

  • Three pointers are initialized with value NULL and then later three instances of the class are created.

But what I don't understand is

  • Where the class objects are created and
  • From where the pointers are getting the addresses to point to.
  • And if I keep any data in private how to access it later then in this implementation because . operator is giving error

1 Answer 1

1

Where the class objects are created

The allocating new-expression acquires the memory from the free store.

From where the pointers are getting the addresses to point to.

The new-expression returns the pointer to the object that it created.

how to access it later

By indirecting through the pointer using an indirection operator.

Sign up to request clarification or add additional context in comments.

4 Comments

Assume that I have put the int data of class node in private and created a function in public named push_data which accepts a int in arguments and modifies the private data.
then should I write like this: *head.data(1);
@SoumalyaBhattacharya You can access private members in member functions.
Then like *head.push_data(1)?? Sorry for annoying you but its getting on my nerves now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.