define a array in a class
hi,
I am a beginner in c++. I created two classes: Ant and Nest. the relationship between these classes are the following.
A nest HAS A set of ants. So I have to add a member which represents this ''set of ants'' in the nest class. right?
my question is how can I represent this set? I think that an array of 200 elements(=200 ants) could be the solution. But what is the best way to define the member of Nest:
1 - create an array of 200 hundreds ants -> Ant ants[200];
2 - create a pointer to an array of 200 ants --> Ant* ants[200];
3 - create a pointer to a ant. Ant* ants;
other ???
what the constructor of ants should look like to?
If you have somothers suggestion don''t hesitate.
thank you
zip 7000
ps : perhaps I did some mistakes in my explanation. Don''t hesitate to tell me.
1. hmm... for beginners... I would suggest ant[200] as it is the simplest (no pointer to shoot your feet) .
2. If you already understood pointers well enough, go for *ant.
3. If you are going to store derivatives of Ant class, say WorkerAnt or ArmyAnt, but not so pointers-friendly... then go for *ant[200].
4. If you are under (2) and (3), you may go for **ant
5. If you in (4) and know STL.. go for std::vector<Ant*> ant.
Just my suggestion.
2. If you already understood pointers well enough, go for *ant.
3. If you are going to store derivatives of Ant class, say WorkerAnt or ArmyAnt, but not so pointers-friendly... then go for *ant[200].
4. If you are under (2) and (3), you may go for **ant
5. If you in (4) and know STL.. go for std::vector<Ant*> ant.
Just my suggestion.
quote:Original post by zip7000
2 - create a pointer to an array of 200 ants --> Ant* ants[200];
I can never remember the exact declaration rules, but I don''t think that does what you think it does. I''m pretty sure that''s declaring an array of 200 Ant pointers, not a pointer to an array of 200 Ants.
You can''t declare a pointer to a fixed array size. A pointer is dumb, it doesn''t know whether it''s pointing to 1 thing or an array of a billion things.
USE A std::vector ! The standard template library''s vector class is much easier, safer, and more usable than using C++ arrays! You can readjust the size of vectors any time, and they offer bounds checking if you want. Here is how to use them in a class
#include <vector>class Nest{// other stuff hereprivate: std::vector m_Ants;}// constructorNest::Nest() : m_Ants(200){// other inits here}// now just act like it was a normal array withm_Ants[10]; // access 11''th elementIts that easy! If you want to learn the more complex aspects of vectors, just search google.
Because DanG didn''t use source tags ([*source][*/source] without *), one of the most essential bits of that code is messed up.
------------
Where''d the engine go? Where''d it go? Aaaaaah!!
MSN: nmaster42@hotmail.com, AIM: LockePick42, ICQ: 74128155
std::vector m_Ants;//isstd::vector<Ant> m_Ants;
------------
Where''d the engine go? Where''d it go? Aaaaaah!!
MSN: nmaster42@hotmail.com, AIM: LockePick42, ICQ: 74128155
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement