[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

multiple instances in a loop

Posted on 1998-03-14
5
Medium Priority
?
256 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 6

Accepted Solution

by:
jpk041897 earned 200 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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…
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.

650 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