Skip to main content
GameDev.net gamedev.net
🔒 Locked

Structs or classes?

Started by DarkEcclipse Apr 8, 2000 at 10:44 PM 29 replies 4.8k views
Original Post
DarkEcclipse
DarkEcclipse
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...
lpsoftware
lpsoftware
It''s all a matter of personal preference. Use what you''re comfortable with or used to already.

Martin
______________Martin EstevaolpSoftware
ZoomBoy
ZoomBoy
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

osmanb
osmanb
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 Foo
class Foo;

// More random code here

// Real declaration of STRUCT Foo
struct 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
Mezz
Mezz
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
Kylotan
Kylotan
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''
mhkrause
mhkrause
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.
Tooon
Tooon
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
DerekDay
DerekDay
Personally I use structs for holding data mostly, as opposed to providing functionality. If I have a really large, complex class/struct, I make it a class. If I have a small data structure, I may add a few member variables to it for convenience, but I make it a struct... It really doesn''t matter though ... it''s just a matter of taste, but conceptually I''ve found this way works well ... whatever gets you up I guess.
-Derek
TANSTAAFL
TANSTAAFL
my general rule (not written in stone or anything) is to use a class for anything that has a member function, and to use a struct for anything that does not. true, i could put member functions in structs, and if i had a class that has all public members, a struct is exactly the same.

my reason for this convention is mainly to keep myself organized... in my programs, structs merely hold data, whereas classes DO stuff.

there are a few things besides members that i will add to a struct... i will add constructors and/or destructors as needed, copy constructor if default copy is not sufficient, any operators that are needed, and usually a conversion operator (if required)
Get off my lawn!
Zipster
Zipster
Ahem, yall, but to all you people who think structs and classes are the same, well....listen to this:
Can you derive structures? I DON''T THINK SO!!

(Hehe, I never tried, but I''m sure I''m right. Plz don''t flame me if I''m not right )
Erik Post
Erik Post
Indeed, put an end to this thread Structs and classes are the same, there''s no performance difference. The only reason structs are in C++ is because C++ had to be compatible with C.

Erik
Torval
Torval
Answer to Zipster.... YES you can dervie structs.

Torval
mossmoss
mossmoss
quote:
Original post by Zipster

Ahem, yall, but to all you people who think structs and classes are the same, well....listen to this:
Can you derive structures? I DON'T THINK SO!!

(Hehe, I never tried, but I'm sure I'm right. Plz don't flame me if I'm not right )



Okay, this is not a flame, but that's incorrect. You can derive from structs. In almost every sense, struct and class are exactly the same.

Section 10.2.8 of the standard reads:

quote:

By definition, a struct is a class in which members are by default public...



And that the former of the following two structs is a shorthand for the latter:


struct s {
// stuff
};

class s {
public:
// stuff
};


Stroustrup goes on to say that "Which style you use depends on circumstances and taste. I usually prefer to use struct for classes that have all public data." He goes into a little more detail, but if you're interested, I suggest you grab the book yourself. (It answers almost every C/CPP question that shows up in these forums.)


Edited by - mossmoss on 4/13/00 4:34:07 PM
Zipster
Zipster
To end this post, I say that structures and classes are the same thing basically, and structures are the newbie, old-fashioned C way, while classes are the grown-up, C++ way.

Kinda like printf and cout . Whats the difference, except for the fact that printf is C and cout is C++. Same for scanf and cin .

THE END

mbarela
mbarela
I think that question falls under the general blanket of what people are talking about in this topic. It''s a personal prefrence. Use whatever you like, unless your employed to do this and then go with what standards you are provided with.

Mike Barela
mbarela@earthlink.net
Mike BarelaMikeB@yaya.com
Kylotan
Kylotan
I like the streams syntax. There''s less redundancy, more type-checking, and it becomes easy to make my own streams and use them in the same way.
Zipster
Zipster
Actually, I like printf over cout since it looks like your old-fashioned function, not this new dang-fangled streaming magigy
mbarela
mbarela
But surely you overload operators at some point? It''s the same thing really.
Mike BarelaMikeB@yaya.com
Dragonskin
Dragonskin
My preference is printf over cout. It just seems easier to use (from a formatted output point-of-view) than cout''s interface. If you look at how you limit the length of outputed variables in cout you might agree with me. You only need to use a number in printf, instead of a function for cout. (At least, that''s what I think it is...it uses parentheses, anyway...)

About the type checking, I will say that cout is probably checked better than printf. At least in VC++ 6.0...I know that DJGPP, with all errors and warnings enabled, will complain if you use the wrong types for a given place in a format string. In VC++ I''ve gotten character strings printed through a decimal format (well, almost...it crashes the program at that point) without so much as a complaint from the compiler.

In any case, as with almost everything, it is personal preference.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.