Jump to content
  • Advertisement
Sign in to follow this  
Instigator

keyword 'struct'

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
As well as inheritance; by default: public for structs, and private for classes.

If jpetrie's post already implies this, then forgive my English. =)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
Guest 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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!