The only difference between a C++ structure and a class is that, by default, the members of a class are private, while the members of a structure are public
Originally posted by: Zugzwang152
link
The only difference between a C++ structure and a class is that, by default, the members of a class are private, while the members of a structure are public
Originally posted by: maddogchen
This reminds me of the lame C++ joke.
Your Mom is like a struct. She's got no class!
Originally posted by: Nothinman
Classes allow 'magic' to happen in the background with private members, contructors, destructors, overloaded operators, etc. structs are just easy ways to group data, but technically they can contain functions as well if you use struct members as function pointers.
I would have to disagree. If you look through the standard library, boost, the creator of C++'s website or book, you will see structs being used to hold functions all the time. Generally they are used for small classes which only contain public things. It's a lot less clutter to just use 'struct' instead of using 'class' and then having to add a 'public:', when your class is only a few lines long.In terms of semantics, structs generally shouldn't contain functions. Mostly because in the C days they couldn't, and were strictly for data grouping type stuff. The structure is kind of useless without this semantic.
Originally posted by: BingBongWongFooey
Originally posted by: Nothinman
Classes allow 'magic' to happen in the background with private members, contructors, destructors, overloaded operators, etc. structs are just easy ways to group data, but technically they can contain functions as well if you use struct members as function pointers.
Sure they can. Didn't you read the stuff above?
I would have to disagree. If you look through the standard library, boost, the creator of C++'s website or book, you will see structs being used to hold functions all the time. Generally they are used for small classes which only contain public things. It's a lot less clutter to just use 'struct' instead of using 'class' and then having to add a 'public:', when your class is only a few lines long.In terms of semantics, structs generally shouldn't contain functions. Mostly because in the C days they couldn't, and were strictly for data grouping type stuff. The structure is kind of useless without this semantic.
Originally posted by: SelArom
aren't classes passed by reference while structs by value?
Originally posted by: BFG10K
I personally would never use structs other than for data.
Originally posted by: torpid
Yes, you see lots of unconventional things in the above. Obviously the creator of C++ is going to use them, otherwise he wouldn't have introduced the concept into C++, since it was not in C.
I wouldn't say that at all.I suppose you could say it's a C++ mentality to do whatever it takes to reduce typing
If you think using:, but real world goals actually offer the opposite. Clarity in code is more important than reduced keystrokes. That's why you would name a function CalculateMortgageRate and not CalcMorgRat
Originally posted by: BingBongWongFooey
Originally posted by: torpid
Yes, you see lots of unconventional things in the above. Obviously the creator of C++ is going to use them, otherwise he wouldn't have introduced the concept into C++, since it was not in C.
It's not unconventional when they use them in documentation and FAQs (and books, etc etc). It's a pretty well accepted practice.
I wouldn't say that at all.I suppose you could say it's a C++ mentality to do whatever it takes to reduce typing
If you think using:, but real world goals actually offer the opposite. Clarity in code is more important than reduced keystrokes. That's why you would name a function CalculateMortgageRate and not CalcMorgRat
struct foo {
instead of:
class foo {
public:
is not clear, then you don't sound very comfortable with C++. It's pretty clear and simple if you ask me.