Archived

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

Chryzmo

Struct and Class Confusion...

Recommended Posts

Chryzmo    122
I posted before about how todeal with global varible in my project and people replied with structs and classes. So, I looked into them and it seems to me that structs are just to hold variables, while classes can hold private variables and functions to access and manipulate those variables. So, my question is, when do I use a struct over a class? Is it bad OO programming to use a struct rather than a class? Also, I have a question about OpenGL, I believe I have read that OpenGL is only for rendering 3D graphics. So, is there a portable API, like DirectDraw, for 2D graphics? Thanks, Chris

Share this post


Link to post
Share on other sites
TravelingCoder    296
In C++, a struct and a class are the exact same thing except structs default to public access while classes default to private. I believe at one point classes were more complex, but not anymore. I am pretty sure opengl can be used for 2d graphics as well as 3d graphics.

Share this post


Link to post
Share on other sites
EL    122
struct and classes are basically the same. The only thing that''s different is that (without using access specifiers) the members of structs are public and those of classes are private.

If you want to use structs instead of classes it''s fine, but I do believe most people prefer using class for genuine datatypes and struct for small, simple types.

Oh yeah, OpenGL can be used to to 2D graphics.

Share this post


Link to post
Share on other sites
biovenger    122
Now note this important issue:

The ONLY difference in C++ between classes and structs is that a struct''s member variables and methods are public by default, where they are private for classes.

Otherwise, they are the same. Structs can hold methods, constructors, overloaded operators, etc...

From my experience and just plain preference I use classes for data that needs to be abstract to go with the OOP paradigm, while I use structs where data needs to be out in the open.

Take this as an example:

If you wish to create a very simple rect class, the most convenient way for any user to acces elements in it is to make them public, thus you will have a x, y, w and h element that is public. (top, left, bottom and right if you prefer)
Now, since there is no need at all to encapsulate the data in a simple rect, you can use a struct by default, so that users know that the data structure is a open entity where all objects are public.
Of course, you can also add a constructor to this struct for easy initialization and some helpful methods such as Area, Width, etc... But make them all public.

I use this simple rule of thumb: "If you have to use either of the keywords; private, public or protected, in the declaration of the datatype, go with a class. If you want ALL data to be public by default use a structure."

Hope that helps.

Share this post


Link to post
Share on other sites
iaretony    127
quote:
Original post by Sneftel
Perhaps you should RTFFAQ.


How appropriate. You fight like a cow.


His question is very on topic. This is the "Beginner" forum. That was rude!

Tony

Share this post


Link to post
Share on other sites
Sneftel    1788
quote:
Original post by iaretony
His question is very on topic.
So was my response.
quote:
This is the "Beginner" forum.
What''s your point? Beginners aren''t allowed to use Google?
quote:
That was rude!

It was rude to give a direct link to an authoritative answer to a commonly asked question, one which both gave the technical answer and a fuller explanation? I don''t think that word means what you think it means.


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
Yog    122
My teacher''s at the university condemn structs. The C++ course even skipped them entirely. My teachers are also OO freaks. But seriously, depending on who you talk to, some say structs and "friend classes" horribly violate OO while others will tell you just do whatever you want as long as your code is neat and tidy and works . So I guess I haven''t answered your question or helped you in anyway but I hope you''re grateful anyways. You''re welcome .

Share this post


Link to post
Share on other sites
evillive2    779
I am assuming that the difference between classes and structs has been adequately answered or researched.

I think you will find that in general, as has been my experience, that the common use for structs are to hold "simple" data types like the RECT struct mentioned above. When you want to encapsulate data types AND you want to encapsulate methods associated with those data types, you would use a class. Now, you can add methods to a struct, but generally people just don''t.

I think (opinion here, not fact) the only reason structs are still around in C++ is for compatability with standard C libraries which use structs and not classes. Originally C++ was just an extension or upgrade of C, although now has become it''s own beast, but who wants to rewrite all of that stuff into classes when it works fine the way it is anyway?

Share this post


Link to post
Share on other sites