Jump to content
  • Advertisement
Sign in to follow this  
chadsxe

How do you declare an array of classes?

This topic is 4084 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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);
}
}


Share this post


Link to post
Share on other sites
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 memory
for ( 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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
STL is part of the c++ standard and is fully cross-platform, only the windows api header files will cause problems

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!