#### Archived

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

# Structs or classes?

This topic is 6515 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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...

##### Share on other sites
It''s all a matter of personal preference. Use what you''re comfortable with or used to already.

Martin

##### Share on other sites
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.

##### Share on other sites
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

##### Share on other sites
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

##### Share on other sites
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

##### Share on other sites
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.

##### Share on other sites
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''

##### Share on other sites
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.

##### Share on other sites
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

##### Share on other sites
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

##### Share on other sites
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)

##### Share on other sites
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 )

##### Share on other sites
quote:
Original post by 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

##### Share on other sites
Well, I think we can end this stupid thread, because structs and classes ARE THE SAME THING, except that structs default to public, while classes default to private, which has been pointed out several times

It''s just a matter of preference when to use structs and when to use classes.

##### Share on other sites
which one is faster?

##### Share on other sites
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

##### Share on other sites
Answer to Zipster.... YES you can dervie structs.

Torval

##### Share on other sites
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

##### Share on other sites
If it''s all public, use a strut. No reason to ahve a class if it''s all public. But how will you manipulate the variables? See I have class player_obj
now the var''s are all private, but the subs are public. The subs manipulate the var''s in teh right way, for raising skills, etc, so to raise a stat all i do is call player.raise(int)....then the sun figures out if the int(exp, skill pts, whatever) is high enough to raise the stat up to the next level. I also have heal and hurt subs for raising or lowering stuff, timed heal and hurt for effects that last only so long, and skill rolls subs that return true/false if the action was performed(like attaking/defense)

I think this approach is much more OOP, but if you aren''t going the OOP route, and all is public, then use a struct.

quote:
Original post by 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...

##### Share on other sites
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

##### Share on other sites
Just to extend the post a little bit more =)

How many does realy prefer cout over printf?

I just can´t stand the syntax for cout..it´s not c syntax it´s something else....printf just looks better

//mats

##### Share on other sites
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

##### Share on other sites
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.

##### Share on other sites
Actually, I like printf over cout since it looks like your old-fashioned function, not this new dang-fangled streaming magigy