Creating multiple objects using For loop

Hey All,

I am trying to create a function which is passed a number indicating the amount of objects to create of a particular class type. While creating these objects, each should have its own pointer name so that each object can be accessed individually at any place throughout the life of the program. I was trying to name each object as 'matrixObject' and add a number in the end. For example, the first object would be matrixObject1, the second would be matrixObject2, and so on. How can I increment the digit in the pointer name while the for loop is running?

I hope I have made myself clear.

Thanks!
hi

There is no way to dynamically create variable names - what you're looking for is an array of dynamically created objects.

Such an array can be created very simply by using the operator new[]. Example showing creation of n Matrix-objects:

 
Matrix* matrixObjects= new Matrix[n];  // Array of size n of Matrix-objects  


The individual objects can then be accessed by their index:

 
matrixObjects[4].transpose();  //..some operation on the 5th Matrix-object 


Just remember to delete the array again when you're done:

 
delete[] matrixObjects;  // Kill'em all! 


hope that helps!

/Corpus
Sounds like a good solution. I'll try it out and report the results here. Thanks!
I tried using the above method. The program is returning some kind of error. When I tried debugging the program, I noticed something strange in the Autos window (I'm using Visual C++ 2005). Here's a part of my code:

1
2
3
4
5
6
7
8
9
Matrix *newMatrixPtr = new Matrix[meshDensity];
for (int n = 1; n <= meshDensity; n++)
{
   meshDensity = meshDensity + 1;
   newMatrixPtr[n].setRowsAndColsRunTime(meshDensity, meshDensity);
   newMatrixPtr[n].AllocateMemory(meshDensity, meshDensity);

   ...


The Auto's window shows that newMatrixPtr and newMatrixPtr[n] have been allocated seperate memory locations. newMatrixPtr's location has a garbage value throughout the program, only newMatrixPtr[n] seems to be getting populated with the values I enter during the program.

Am I missing something here?
Last edited on
Errr.... Line 2 is very wrong. The first thing they teach you is that arrays start from 0, not 1.

for (int i = 0; i < meshDensity; ++i)
Zaita,

Thanks for the correction. I did that intentionally because I wanted to associate the pointers with element numbers so that the element count could start from 1 and not 0.

I corrected the problem you identified by the actual issue was not resolved.
Line 4 will cause the loop to never end.
helios, you're right! I subtracted 1 at the end of the loop and there has been some improvement in the program. Thanks!
'for' looping statement has three parameters..........
I was able to fix my problem by correcting the problem in the for loops identified above. Thanks all!
You shouldn't start the utilization of your array from 1. This is just retarded.
you are using Pointer so this is used ( " -> ") insted of dot operator ......

and there is infinite loop here so chech it out !!!
meshDensity = meshDensity + 1;


should not be here iand it is good programming practice to not to use the condition statement in loop for ammendments ...
and Farhan i am from Lahore .. :)
Topic archived. No new replies allowed.