Link to home
Start Free TrialLog in
Avatar of wong83
wong83

asked on

Quickie Dynamic memory

Hello Friends,
this is my sample code.
#include <iostream.h>

class Node
{
public:
Node(int);
private:
     int data;
     Node * next;
};

Node::Node(int n)
{
     int x;
     x=n;
}

void main()
{
     int nod;
     cout << "Enter node";
     cin >> nod;

     Node *Head;
     Head = new Node(nod);***********************

}

the line marked with ***'s
after i compile and run this say i enter 3 for nod.
does this mean something like Head[3].

i'm trying to ask the user the number of nodes he wants and then create them dynamically.
is this correct?
Thank you .
Wong
Avatar of n_fortynine
n_fortynine

based on what you wrote 3 will be assigned to a var x that is created in your constructor n that'll be it. I guess the right thing would be Node::Node(int n) : data(n) {}. In this case then you would be new-ing a Node which contains the data 3.
So it would not be correct for Head[3]. I guess you're trying to build a linked-list-like structure???
ASKER CERTIFIED SOLUTION
Avatar of n_fortynine
n_fortynine

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wong83

ASKER

Thanks n_fortynine.
You're welcome. Does 83 in your username imply anything?  It's my year of birth =).
Avatar of wong83

ASKER

It was mine too about 4 yrs ago . i've had that(usrname) for a long time so i decided to keep it probably because i don't have to waste a lot of my memory blocks. Thank you for your help.