How do you declare an array of classes?

Started by
10 comments, last by chadsxe 16 years, 11 months ago
I declare the class like so Character Hero(graphics, collision, 100, 100); I want to make an array of Character class objects... I tried... Character Hero[10](graphics, collision, 100, 100); But that does not work. Any suggestion would be greatly appreciated. Regards Chad
Advertisement
What language are you using?

I'm going to hop out on a limb and guess C++, in which case you may find this FAQ useful.



Also, thread moved to For Beginners.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

I am using c++...checking link now.
Short answer:
Character Hero[10];

This will initialize an array Hero with 10 elements. Each element will be of type Character. So when the array Hero is created, it initializes each element to the default constructor of the type Character. So if you havn't declared a default constructor for your class Character, then all the data members will be set to the default, or 0. And if you wan't to initialize each element in the array to the same thing, you would write
Character Hero[10] = { graphics, collision, 100, 100 };

(Assuming that graphics, collision, 100, 100 are all valid)
try

Character Hero[]={Hero(graphics, collision, 100, 100),Hero(graphics, collision, 100, 100),Hero(graphics, collision, 100, 100),Hero(graphics, collision, 100, 100),Hero(graphics, collision, 100, 100),Hero(graphics, collision, 100, 100),Hero(graphics, collision, 100, 100),Hero(graphics, collision, 100, 100)};//though a array of pointers would be better//not sure i got this right though,Character **Hero;void init(){Hero = new *Hero[10]  for (int i=0;i<10;i++){    Hero= new Hero(graphics, collision, 100, 100);  }}
Unfortunately, there's no concise syntax in C++ for doing what you are trying to do with a class that requires arguments passed in the constructor.

You'd have to do it either like this (using pointers and dynamic memory allocation):
Character *Heroes[10];for ( int i = 0; i < 10; ++i ){    Heroes = new Character(graphics, collision, 100, 100);}//remember that when you're done with those objects you now have//to release the memoryfor ( int i = 0; i < 10; ++i ){    delete Heroes;}



or you could use an STL container:
Character *generateHero(){    //graphics & collision need to be accessible at this scope    return new Character(graphics, collision, 100, 100);}std::vector<Character*> Heroes;std::generate_n( Heroes.begin(), 10, generateHero );


I'm guessing that both methods utilize parts of C++ that you haven't learned yet: dynamic memory allocation on one hand, and also the STL on the other.

If Character is small, and has a default constructor (a constructor that takes zero arguments) and you have a properly overloaded operator= then I suppose you could also do:

Character Hero[10];Character heroTemplate(graphics, collision, 100, 100);for ( int i = 0; i < 10; ++i ){    Hero = heroTemplate;}


However, in general, you don't often make arrays of large classes without referencing them in the array by pointer.

-me
I wish I could use STL but I am doing this cross platform (i.e. in windows and Linux). I don't want to take the chance of things messing up. I will try the examples above and let everyone know the results

Thanks

Chad
STL is part of the c++ standard and is fully cross-platform, only the windows api header files will cause problems
Quote:
I wish I could use STL but I am doing this cross platform (i.e. in windows and Linux).


This makes me laugh. The SC++L (what you really mean when you say STL) is standard. It is part of the language; it is just as cross-platform as the rest of the language... unless you misuse it (but that applies equally to everything in C++), you'll get the behavior it guarantees you.

So use the SC++L. Don't make things harder for yourself based on speculative misinformation.

Quote:
I don't want to take the chance of things messing up.

Technically, you probably have a better chance of "things messing up" if you use standard C++ data types (char, short, long, int, et cetera) than the SC++L, because the standard defines so very little about those types in practice (only relative sizes, and that their size is expressed in multiples of sizeof(char), which is guaranteed to be 1, but that doesn't mean its one byte, et cetera).
Quote:Original post by Shakedown
Character Hero[10] = { graphics, collision, 100, 100 };



This is not valid C++ syntax. The correct way to initialize a static array of objects using a list is:

Character Hero[10] = { Character(graphics, collision, 100, 100),                        Character(graphics, collision, 100, 100),                       Character(graphics, collision, 100, 100),                       Character(graphics, collision, 100, 100),                       Character(graphics, collision, 100, 100),                       Character(graphics, collision, 100, 100),                       Character(graphics, collision, 100, 100),                       Character(graphics, collision, 100, 100),                       Character(graphics, collision, 100, 100),                       Character(graphics, collision, 100, 100) };


Using an array (or an STL container) of pointers would be a better approach, though.

This topic is closed to new replies.

Advertisement