Solved

multiple instances in a loop

Posted on 1998-03-14
5
217 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Building cUrl in Windows v7.43.0 4 28
how to convert c++ code to Android App 3 83
Why isn't object file created? 6 42
Copy output image from TWindowsMediaPlayer 6 23
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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 clear a vector as well as how to detect empty vectors in C++.

867 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