• Announcements

Archived

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

Long standing question using class objects.

Recommended Posts

Hello, I am not a total newbie, but I am sorta when it comes to classes. My dilemma I have always been curious about is a situation like this: If you have a enemy structure say and want 100 enemies in your game u typically do that like: typedef struct { int x; int y; ...etc.. } ENEMY ENEMY theEnemies[100]; But what about when you have an Enemy class how do you do 100 enemeies? Do you have methods to that class to the enemies class can handle its own data in a linked list or something? or do you really have to do this: CEnemy theEnemy1 CEnemy theEnemy2 CEnemy theEnemy3 ... CEnemy theEnemy100 That seems absurbed to me, but how do you know which enemy in a list of class enemies you are dealing with? Thanks for your help, I hope this post made some sense. Regards, Shane

Share on other sites
You can do it the same way you do it with structs.

CEnemy theEnemy[100];
theEnemy[0].x = 50;
theEnemy[0].y =20;
etc.

~~~~~~~~~~
Martee

Share on other sites
holy moly, i feel dumb. So you can really do it all the same way?

i.e.

CEnemy theEnemy[100];

theEnemy[0].RenderMe();
theEnemy[0].Collide(theEnemy[1].GetBoundRect());

etc...? thats pretty nifty, now I have no excuse for not using C++

Thanks a lot

Shane

Share on other sites
Something to remember in C++: classes and structs are exactly the same, except for default access specifier. The default for class is private, and the default for struct is public. Other than that, they are identical; you can do with one what you can do with the other.

Share on other sites
So in effect I could make a link list of classes etc...

Then I am curious (this is probably blasphamy) but whats the point to using classes exactly? If you can just do it all with structs etc.. ? Just curious. Isnt C++ over head and over kill for say a small game project (of course not a 3d engine).

Shane

Share on other sites
Yeah, there's no fundamental difference

class Blah {   int m_blah;   virtual void Dumbfunction()=0;};

is equivalent to this, in C:
struct {struct {void (*Dumbfunction)();} *lpVtbl;int m_blah;};

Thus, there's no point to using C++

Edited by - Sephiroth2 on July 2, 2001 6:20:00 PM

Share on other sites
OOP, we have turned another one. Welcome to the dark side.

Share on other sites
quote:
Original post by Codejoy
Then I am curious (this is probably blasphamy) but whats the point to using classes exactly? If you can just do it all with structs etc.. ? Just curious. Isnt C++ over head and over kill for say a small game project (of course not a 3d engine).

Show me C++ overhead, and I''ll show you you''re imagining things. =) We''ve already done this in another recent thread--there''s exactly the same number of instructions to call a member function as there is to call a "C API" free function that operates on a struct.

One of the only valid arguments for extra overhead is in virtual function calls, and if you apply virtual functions to solve the correct problem, there''s no overhead when compared to the analog in C. There are other arguments, most of which regard templates, and some of which are actually valid.

Anyhow, C++ can do everything C can do, and if you start with the baby-step of using member functions, your code will execute with the exact same number of instructions as it would have using free functions. I don''t believe it''s overkill for any project to use C++.

As for when to prefer classes over structs or vice versa, it''s a matter of style but generally:
- classes are used when you want to hide private or protected data; in this case, there is usually no public data.
- structs are used when all data and members are public.

It makes not a bit of difference, other than having to type an additional public: or private: tag depending on which you choose.

• Partner Spotlight

• Forum Statistics

• Total Topics
627677
• Total Posts
2978590

• 11
• 12
• 10
• 12
• 22