Advertisement

Structs or classes?

Started by April 08, 2000 10:44 PM
29 comments, last by DarkEcclipse 24 years, 8 months ago
Hey all, I''ve been sitting up at my ''puter all night, and have been thinking... which is better for holding rpg stats? Structs, or classes? I have Visual C++, so here''s what I came up with: First, the struct: struct rpgCreature { char Name; char Class; int stregnth; int dexterity; int health; int magic; // or whatever ability name you like } And, the Class: class rpgCreature { public: char Name; char Class; private: int stregnth; int dexterity; int health; int magic; } I could actually specify exactly how the class looks, but my fingers are beginning to revolt... What do you think? DarkEcclipse...
DarkEcclipse...
It''s all a matter of personal preference. Use what you''re comfortable with or used to already.

Martin
______________Martin EstevaolpSoftware
Advertisement
structs and class are essentially the same except that the structs attributes and methods are public when not specified and a classes attributes and methods are private when not specified.
Structures look the same as classes but with classes, you get better initialization and deletion of data; everything is more organized. Later on when you want to differentiate between different basic types of character(flying vs. swimming vs. 4 legged vs. 2legged) you can derive the class.
Later on as well you''ll have to do error-checking if you''ve got many characters and it''s easier to wrap that in a function in a class.

As for private versus public, if you''ve got complex adjustments to a character''s data then private is the way to go. If you are just accessing them directly, without any error- or range-checking, then make them public.

ZoomBoy
A 2D RPG with skills, weapons, and adventure.
See my character editor, Tile editor and diary at
Check out my web-site

I''m sorry ZoomBoy, but everything you said is completely wrong. In C++, class and struct are exactly the same. As the anonymous poster pointed out, the only difference is in default protection mode for members. Other than that, they are the same thing. For purposes of declaring/defining data structures, the keywords are interchangeable. (The only exception being the nonstandard use of class to identify template type arguments.) In fact, the following is legal:

// Forward declaration of CLASS Fooclass Foo;// More random code here// Real declaration of STRUCT Foostruct Foo : std::vector{  Foo(int argument=5) { ... }  //More code here}; 


I''m not saying this is a good idea, but it works. And yes, because (like I said) struct and class are the same thing, you can have structs with constructors, inheritance, etc...

Unfortantely, so many people with a C background view structs and classes as being very different. This isn''t helped by standard practice, where people always use struct for small chunks of data, and class for anything larger. I still tend to separate, but I draw the line higher, so things like STL function objects I generally declare as structs (but they have operloaded operator(), and usually have constructors too.)

Hope this helps.
-Brian
Ozman has it right, In C++ they are the same apart from defaulting to public scope. It would seem that some people like to use structs for ''plain old data'' types, as this is what they were originally used for, and use classes for all the stuff like inheritance, member functions etc. You *can* do all these things with structs, but it''s preference mainly with C++, and I prefer classes for complex heirarchies.

-Mezz
Advertisement
So, it looks like the performance may be the same. From a design perspective, think about readability -- a lot of code constructs exist mostly for conceptual simplicity. I''ve usually seen structs used for holding collections of data that don''t do anything, as an RPG player''s statistics would. If you want to have a class system, you could treat your stats struct as a member of your CBaseCreature class. Then your other creatures, derived, would inherit the struct. Yes, you could have a struct with a member struct that is used to derive other structs, but your team members would hopefully beat you senseless

I would be really careful about doing things like declaring string pointers in your struct if it''s going to be a member type.
Generally, I use classes for everything except a relatively small number of cases where I want to stick a few basic C data types together, such as int, char, etc, and I know I will need no bounds checking, initialisation, etc. In my code, ''struct'' means ''nothing interesting to see here''
The rule I follow is this: If the implementation is the same as the interface, use a struct. Otherwise, use a class.

Basically, that means that if the object holds nothing but data, and the only operations are reading or setting the data, a struct should be used. Otherwise, a class.
As all of the member of your Class(struct) will be public,
use the struct. And, Yes! A struct is just a class with the
public accessibility of the members as default.
I think this is matter of taste, use what is convenient for you.

/ Tooon

This topic is closed to new replies.

Advertisement