Advertisement

Structs or classes?

Started by April 08, 2000 10:44 PM
29 comments, last by DarkEcclipse 24 years, 8 months ago
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
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!

Advertisement
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 )
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


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.
which one is faster?
Advertisement
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
Answer to Zipster.... YES you can dervie structs.

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


This topic is closed to new replies.

Advertisement