0

Hi I am from a Java background and quite new to C++ In my intermediate level of Java programming i never had situation where i declared an object of some class within the body of that class, now am studying linked lists in C++, here in this code and in several other places i have come across that pointer to that class/struct is declared within its body i.e Struct ListNode * next; in the following code

private:
// Declare a structure for the list
structListNode{
float value;
structListNode*next;
};
ListNode*head; // List head pointer
public:
FloatList(void) { // Constructor
head = NULL;
}
~FloatList(void) { }; // Destructor
void appendNode(float);
void displayList(void);
void deleteNode(float);
};

or Node * next ; in this code below

class Node {

    int data; // The value stored in node
    Node* next; // The address of next node
    }

I can'really comprehend what it means or how is it going , can someone please explain it to me?

Ali M
  • 47
  • 3
  • You should learn about what _pointers_ are and how do they work. The code doesn't declare _"an object of some class within the body of that class"_. It declares _a pointer to an object of some class within the body of that class_, which is different. A [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) might help. (Imagine that a pointer is basically an address. Why there should be any problem with storing an address inside a class instance?) – Daniel Langr Dec 22 '20 at 21:45
  • 1
    In Java this would be equivalent to : `class Node { int data; Node next;}`. It means `next` refers (in java) and points (via pointer) in c++ to instance of Node – marcinj Dec 22 '20 at 21:47
  • Do you understand the concept of a linked list? Have you seen any of the myriad of good visualizations out there? A `Node` with a pointer to another `Node` is the hallmark of that data structure. – scohe001 Dec 22 '20 at 21:47
  • I do get it that its not an object within the body of the class/struct , but how thing are going in memory when i make pointer of a class within the body of the class? how it works ? – Ali M Dec 22 '20 at 21:50
  • @AliM _"How it works?"_ — This very much depends on what you assign to that pointer. In your case, just google "C++ linked list". – Daniel Langr Dec 22 '20 at 21:52
  • @AliM here's one of the many MANY great visualizations out there. It's even got member names matching yours! ([image link](https://res.cloudinary.com/practicaldev/image/fetch/s--dTKLdnEj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/mt5yv6qjod33bd37cr0o.jpg)) – scohe001 Dec 22 '20 at 21:54

1 Answers1

0

The pointer inside struct/class is not an "object" of that struct/class, instead, it is a pointer of the objects of the same type. It just stores/holds address of objects created somewhere else, so it can access or modify them without copying their content.

As an example;

class Node {
  int data;
  Node* next;
};

int main(){
  Node* head = NULL; // This is a pointer, not an object.
                     // It points to NULL (i.e. nothing) yet.

  Node n1, n2; // These two are "objects" of Node class

  head = &n1; // head now points to n1, i.e. holds memory address of n1
  n1.data = 10; // n1.data contains 10
  n1.next = &n2; // n1.next points to the object n2

  n2.data = 20; // n2.data contains 20
  n2.next = NULL; // n2.next points to nothing

  head->data = 5; // now, n1.data contains 5, instead of 10
  head->next->data = 15; // now, n2.data contains 15, instead of 20

  return 0;
}
msuzer
  • 51
  • 1
  • 8