Jump to content
  • Advertisement

Archived

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

wise_Guy

Structs to Classes

This topic is 6650 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

After using Andre LaMoth''s examples from Windows Game Programming for Dummies, I am at a loss. In the book, LaMoth uses structs for things like sprites and bitmaps, and defines them like this: typedef struct sprite_typ { --- --- }, SPRITE, *spritePtr and then he can write functions like this: int drawSprite(spritePtr sprite, bitmapPtr bitmap, etc etc) ......... Okay, now here''s where the problem arises. I am now writing my own game and would like to use classes for most of the functions, (ie sprite.draw, object.update, as directed by someone here at game dev). Now when I am making a class method, I am unable to convert some old functions (such as grab bitmap) I thought that i could just do: int grabBitmap(*sprite currentSprite, etc etc) where sprite is the name of a class, but for some reason it is not the same, because if I use the STRUCT version of sprites and grabbing bitmaps, the program works, but if I use CLASSES the program hangs when trying to perform the GrabBitmap function. Is there a difference between *spritePtr as a typedef and just using *sprite in the function prototype? Has anyone else had this problem? Thanks in advance, wiseGuy

Share this post


Link to post
Share on other sites
Advertisement
Classes aren''t always the best approach. Sometimes it is better to use a struct in your situation. Remember that you have the whole language at your dispense and shouldn''t disregard pieces. I must admit that I use to try making everything classes once, but I found out through others, and through trial and error, that this is not always the best approach.

Share this post


Link to post
Share on other sites
Hej,

As soon as you see yourself writing only set and get methods with a particular class then you are better of in a struct. But back to the topic since a sprite is perfectly suited to be put in a class.

If you do:

typedef struct sprite_typ
{
----
----
} SPRITE, *spritePtr;

then the following is exactly the same:

class SPRITE
{
----
----
};

with the only difference that all members in the class are private by default and everything in the struct is public. As a matter of fact, that is the only difference for the compiler as well. For the rest structs and classes are exactly the same. I think your particular problem is in here:

You wrote the following code:

int grabBitmap(*SPRITE currentSprite, etc etc)

while instead you should do:

int grabBitmap(SPRITE *currentSprite, etc etc)

Your * was on the wrong place.

Jaap Suter

p.s. If this is not your problem then ask again


Mmmm, I'll have to think of one.

Edited by - s9801758 on 3/8/00 3:15:31 AM

Share this post


Link to post
Share on other sites
Another approach to this would be to get rid of the struct and put the contents in a sprite class, e.g.

class Sprite
{
private:
void *Buffer;
int x, y; etc...
public:
int grabBitmap();
etc...
};

so when you call grabBitmap, you don''t need to pass any of these parameters in, and if you want to create multiple sprites, just create multiple instances of the class.

Hope this helps.


--Antz
Quanta
http://quanta.virtualave.net

Share this post


Link to post
Share on other sites
Just one note, though -- since games are speed-intensive, you may want to stick with public variables (either use a struct, or declare your class member variables as public). Calling functions takes more cycles than simply referencing a member variable, so, as s9801758 pointed out, it is many times in your best interest to simply use structs. I personally only use private variables when checking their type or manipulating them absolutely MUST be done by a function.

-Chris

---<<>>---
Chris Rouillard
Software Engineer
crouilla@hotmail.com

Share this post


Link to post
Share on other sites
Using an accessor function isn''t slower than using a public member variable. Any function implemented in the class header will be inlined.


class Public {
public:
int x;
};

class Private {
private:
int x;
public:
int GetX() const { return x; }
};

Public a;
Private b;

int z1 = a.x;
int z2 = b.GetX();


Both of the assignment calls at the end will generate the same assembly code and thus execute in the same amount of time. Setting up accessor functions in your classes makes it much easier to modify the class at some later time and not have to search back through all of your code looking for everyplace you use a particular public variable.

Share this post


Link to post
Share on other sites
Finally, as a note to those that are unaware... struct and class are identical. They mean the EXACT same thing in C++, with the only difference being the default protection scheme. You can even write:

class Foo;

struct Foo
{
// Do stuff here
};

They are one and the same.

-Brian

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!