Advertisement

Structs to Classes

Started by March 08, 2000 02:54 AM
5 comments, last by wise_Guy 24 years, 9 months ago
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
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.

Advertisement
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
____________________________Mmmm, I''ll have to think of one.
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
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
---<<>>--- Chris Rouillard Software Engineercrouilla@hotmail.com
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.

Advertisement
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

This topic is closed to new replies.

Advertisement