Advertisement

Stupid newbie question...probably

Started by August 05, 2001 01:08 AM
2 comments, last by MelvinElvin 23 years, 6 months ago
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.
GSACP: GameDev Society Against Crap PostingTo join: Put these lines in your signature and don't post crap!
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
Advertisement
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.whatever2 = value2;<br> spaceMonster.whatever3 = value3;<br>}<br><br>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…<br><br> </i> <br><br>"And that''s the bottom line cause I said so!"<br><br>Cyberdrek<br><a href="http://headhuntersoft.cjb.net">Headhunter Soft</a><br>A division of <a href ="http://dlcmultimedia.datablocks.net">DLC Multimedia</a><br><br>Resist Windows XP''s Invasive Production Activation Technology!<br><br>"gitty up" – Kramer
[Cyberdrek | ]
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

This topic is closed to new replies.

Advertisement