Solved

multiple instances in a loop

Posted on 1998-03-14
5
204 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Sure, I'll send you the code Monday evening (CST)
0
 
LVL 6

Expert Comment

by:jpk041897
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now