Quantcast
Channel: How to initialize an array instance of the class within it self? - Stack Overflow
Viewing all articles
Browse latest Browse all 2

How to initialize an array instance of the class within it self?

$
0
0

I want to be able to create an array of Nodes, within a constructor of Node. However i am getting some kind of heap error.

I wrote a Node class with a constructor trying to instantiate Node array with new.

 class Node 
 {
   private:
     Node* nodes;
     int max_size;
     int current_size;
   public:
     Node()
     {
       max_size = 10;
       current_size = 0;
       nodes = new Node[max_size]; // Error is here*
     }
 };

I am hoping to create an object that creates 10 instances of that object within a constructor. As I am writing this i realized that this will create an infinite loop. Those 10 will create another 10 each, and so on. How should i go about solving this? Should i make a double pointer?


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images