Struct and Class Confusion...

Started by
8 comments, last by Chryzmo 20 years, 8 months ago
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
Advertisement
Perhaps you should RTFFAQ.

How appropriate. You fight like a cow.
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.
And Dave did see the ad of penis enlargement, and gazing upon His holy manhood, He did thrust the spam into the firey depths of hell.- Book of Dave, 1:7
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.
el
I recommend SDL.

edit - misread post

[edited by - jmg5 on August 7, 2003 3:44:18 PM]
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.
-----------------------------Final Frontier Trader
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
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.
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 .
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?
Evillive2

This topic is closed to new replies.

Advertisement