Search code examples
c++objectruntimecreation

How to generate serial objects at runtime?


Let me explain best with an example.

Say you have node class that can be used for a binary tree. You are given data from a file of unknown length containing integers and you just want to place the nodes in the binary tree.

How is it possible to create at runtime a node object with a different name(or memory address) to hold each integer value. For example at runtime you would have to create an object called node_1 to hold the first integer and make it the root, then create an object called node_2 to add it as a child to the root, then node_3 to hold the third integer and so on...

If I was to use a while loop and do node temp* = new node; would this solve the problem and create as many objects as I need with different memory addresses, and I hold each memory address in a vector type?


Solution

  • For future reference. Yes new does create new objects with different memory addresses (therefore does not overwrite old objects).

    Here is the example. http://ideone.com/SbHd0