Solved

multiple instances in a loop

Posted on 1998-03-14
5
232 Views
Last Modified: 2010-04-10
I need to create multiple instances of a class inside a loop (reading text input)... say the call  
A_class varname(input)
is the call to construct an instance of class A_class, how can I do this in a loop (since I figure that calling the constructor with the same variable name will cause errors...variable already declared...)

As you can guess, I'm a beginner to C++, so please submit clear answers...

Thanks in advance
0
Comment
Question by:rafy
  • 3
  • 2
5 Comments
 
LVL 6

Accepted Solution

by:
jpk041897 earned 100 total points
ID: 1183483
To keep things simple, you could use an array:

A_class varname[20];

for (int i =0; i<=19; i++){
   varname[i] = new varname(input) ;

Another option, though much harder to code, is to create an additional static class thats instantiated in A_class's constuctor and which maintains an instance count. Using the instance count, A_class could create space on the heap for all (and I do mean all) its member variables. The addresses of said variables could then be stored in an array or linked list in the static class.

This second option is realy a pain to implement and would only be necesary in very few cases. As a beginner, I would suggest you use an array if you know the loop size or a linked list if you don't
0
 

Author Comment

by:rafy
ID: 1183484
Your first option doesn't solve my problem since I would have to know a maximum number for the size of the array, and I don't since input is picked up in a while loop... the input might create 0,1, or  >1000000 instances, so declaring an array of size 1000000 is not usefull...

I guess would I need to implement it with your other option... could you send me some sample code for the implementation of this with a static class (preferred) or a linked list
0
 

Author Comment

by:rafy
ID: 1183485
Actually, would it be possible to get the implementation as a linked list... I just figured that it may be more useful for me,...thanks
0
 
LVL 6

Expert Comment

by:jpk041897
ID: 1183486
Sure, I'll send you the code Monday evening (CST)
0
 
LVL 6

Expert Comment

by:jpk041897
ID: 1183487
Sorry about the delay got swamped with work. I'll post you a simple linked list class tonight or tommorow with suggestions on how to implement your stuff in it.

0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
object modeling - load and save functions 5 62
Compile GLUT with Visual Studio 2015 1 177
C++ help/ Toy problem 19 31
FMX TEdit KeyUp handler detecting  "enter" key 4 14
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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 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.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

830 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