Solved

multiple instances in a loop

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

724 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