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

keyword 'struct'

Started by Instigator Oct 14, 2006 at 10:04 AM 8 replies 1.1k views
Original Post
Instigator
Instigator
I've read a couple of books on c++ and I've learned about classes, pointers inheritance... well you get the point. No where in these books does it talk about using the keyword 'struct' yet I see it in a lot of programs. Can someone please explain how struct is used... and if it's used in the c++ standard. To my understanding its a lot like a class. However, what's the difference?
jpetrie
jpetrie
In C++, "class" and "struct" differ only in that class members have private access by default, and struct members have public access by default. That it all.

Because of these differences, it is a fairly common idiom to use structs for POD types (nothing but data) since you don't have to specifically declare that the members are public, as you would with a class. This is just an idiom, however.
raz0r
raz0r
As well as inheritance; by default: public for structs, and private for classes.

If jpetrie's post already implies this, then forgive my English. =)
snk_kid
snk_kid
Quote:
Original post by jpetrie
In C++, "class" and "struct" differ only in that class members have private access by default, and struct members have public access by default.


And inheritance for struct is public and private for classes by default

@Instigator - There is no real difference between the two, a class can be a POD-type and a struct can be a NON POD-type, it makes no difference which you use. I just use which ever requires me to type the least amount.
Platinum314
Platinum314
Structs are a relic from C, before there were classes. They are user defined groups of data, and originally could only hold data. By the time of C++ they could contain functions as well, and are practically the same as classes, except structs are public by default, so that old C code will work (most of the time).
The sentence below is true.The sentence above is false.And by the way, this sentence only exists when you are reading it.
Evil Steve
Evil Steve
Quote:
Original post by Anonymous Poster
A C++ book that does not explain structs? I'd throw that book away immediately, since if the author does not bother to explain such a basic feature on a programming language, what other kinds of information the author has "chosen" to omit?

Structures are not a "relic" from C. Not everything has to be a class.
Structs are a C concept, and there's quite a few C++ books that assume you're coming from a C background.

Although I admit it's suprising the book doesn't even mention them.
mikeman
mikeman
As others have said, there is no real difference between them except the private/public thing. But it's better to use the keyword "struct" only when you deal with POD types, since that communicates to most other programmers "only data", whereas "class" communicates "data and methods". For example, this program is completely legal:

class Point2D{public:  float x,y;  };struct GameCharacter{  GameCharacter()  {    Move(0,0)  }  void Move(float x,float y)  {    m_pos.x=0;    m_pos.y=0;  }private:  Point2D m_pos};


But to many C++ programmers(including me), it's confusing and just doesn't "feel" good. We expect things like Point2D to be a struct(unless it also has methods, like a constructor or overloaded operators for instance) and things like GameCharacter to be a class.
Absolution
Absolution
I don't agree that structs have to be PODs only. I'll use a struct with methods when it's a helper data structure to another class. It conveys that it's not a full class to be presented to users.
Fruny
Fruny
Quote:
Original post by Absolution
I don't agree that structs have to be PODs only.


They don't, that's why there is a separate name for POD types. [smile]

Quote:
I'll use a struct with methods when it's a helper data structure to another class.


A POD type can have public member functions.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

Topic Locked

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

Sign in to reply to this topic.