• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# Stupid newbie question...probably

## Recommended Posts

MelvinElvin    122
Straight to the point, How do you invoke the constrcutor on an array of object? In my code, CMonster spaceMonsters[25] (1.0f, 1.0f, 1) ; But the compiler (VC++ 6) doesn''t like it. Thanks for a response. (BTW, i want all the objects to be constrcuted with the same thing). MelvinElvin.

##### Share on other sites
cyn    138
i think there is 2 ways to do this. example...you have an array of int called A. A[5] = {1, 2, 3, 4, 5}; so in your case you would have to do something like CMonster spaceMonsters[25] = {(1.0f, 1.0f, 1),(1.0f, 1.0f, 1).......}; i could be wrong on that. personally i would use something like this.

const int MAXMONST = 25;
//main
CMonster spaceMonsters[MAXMONST];
for(int i = 0; i < MAXMONST; i++)
spaceMonsters.setValues(1.0f, 1.0f, 1);

that way if you need to change the number of space monsters, you only have to change the constant, and not all of your hardcoded values. all you need to do is make a mutator function to act like a constructor. hope this helps.

cyn

##### Share on other sites
Cyberdrek    100
quote:
Original post by MelvinElvin
Straight to the point,

How do you invoke the constrcutor on an array of object?

In my code,

CMonster spaceMonsters[25] (1.0f, 1.0f, 1) ;

But the compiler (VC++ 6) doesn''t like it.

Thanks for a response. (BTW, i want all the objects to be constrcuted with the same thing).

MelvinElvin.

If I remember correctly, you can not implicitly define an array of objects. You need to initiate it using a for loop or something similar..

ie:

CMonster spaceMonsters[25];

for ( i = 0; i < 25; i++ )
{
spaceMonster.whatever1 = value1;
spaceMonster[i].whatever2 = value2;
spaceMonster[i].whatever3 = value3;
}

but then again, I might be completely wrong. I''m not sure. I''ve never really worked with arrays of objects. I''ve always used Linked lists instead...

"And that''s the bottom line cause I said so!"

Cyberdrek
A division of DLC Multimedia

Resist Windows XP''s Invasive Production Activation Technology!

"gitty up" -- Kramer

##### Share on other sites
Null and Void    1088
You can try this (to modify Cyberdrek's example):
  CMonster spaceMonsters[25];for(i = 0; i < 25; i++) { spaceMonsters[i] = CMonster(value1,value2,value3);}

[Resist Windows XP's Invasive Production Activation Technology!]

Edited by - Null and Void on August 5, 2001 2:40:18 AM