Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

array of pointers to a class

Posted on 2001-06-19
5
Medium Priority
?
195 Views
Last Modified: 2010-04-02
I have a class defined as follows:



class ADNode
{
public:          
     int count;
     int ADNcount;
};
 

What I would like to do is to dynamically create an array of pointers to the ADNode class.  For example, if I knew the size of the class array, I would initialize it as follows.

ADNode *ADN;
ADN = new ADNode[MAX_COUNT];


However, I do not know the value of MAX_COUNT at run time.  I will only know the value at the end of the execution of the program.

My question is, that through a do loop or some type of iteration procedure, how can I incrementally increase the size of the array to the class without loosing information?

I would guess that for the first iteration, I would have:

ADN = new ADNode[0];

and for the second;

ADN = new ADNode[1];

etc...

Please advise on how to solve this problem.

Thanks



0
Comment
Question by:dmontgom
  • 2
  • 2
5 Comments
 
LVL 3

Expert Comment

by:Shay050799
ID: 6206690
what about linked list ?
0
 

Author Comment

by:dmontgom
ID: 6206701
I do not know how to do linked lists.
0
 
LVL 3

Accepted Solution

by:
Shay050799 earned 400 total points
ID: 6206713
class ADNode
{
public:          
    int count;
    int ADNcount;
    ADNode* next;
}*first, *curr, *Last;
void main(void)
{
first = new ADNode;
first->next = NULL;
curr=first;
while(....)
{
last = new ADNode;
curr->next = last;
curr=last;
}
}
0
 

Author Comment

by:dmontgom
ID: 6206777
Great answer.  Thanks

I am going to work with it and I might have one more comment left later today or tommorow to ask you.  Then I will grade it.

Thanks,

David
0
 
LVL 4

Expert Comment

by:IainHere
ID: 6206931
Shay's answer is spot on, of course, but if you find that your implementation of a linked list needs much more functionality than has so far been described, I would suggest you check out the STL, which has implemented linked lists for you already.

Only a suggestion for the future - I hope it might be of some use.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question