Difference between a Class and a Structure? (C++)

BigToque

Lifer
Oct 10, 1999
11,700
0
76
I know that a class is user defined and an object is an instance of a class.

What is a struct? (it seems pretty similar to a class)
 

BigToque

Lifer
Oct 10, 1999
11,700
0
76
I just found this...

"In C++, a structure is the same as a class except that its members are public by default."
 

torpid

Lifer
Sep 14, 2003
11,631
11
76
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.
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
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.
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
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? :p

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.
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.
 

torpid

Lifer
Sep 14, 2003
11,631
11
76
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? :p

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.
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.

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 suppose you could say it's a C++ mentality to do whatever it takes to reduce typing, 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
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
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 suppose you could say it's a C++ mentality to do whatever it takes to reduce typing
I wouldn't say that at all.

, 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
If you think using:

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.
 

torpid

Lifer
Sep 14, 2003
11,631
11
76
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 suppose you could say it's a C++ mentality to do whatever it takes to reduce typing
I wouldn't say that at all.

, 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
If you think using:

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.

The only thing clear above is that one is a class and one is a structure. What isn't clear is why the person chose a structure instead of a class. Realistically it makes code more readable to put public: and private: in there anyway, especially given the variance among developers as to which go first in a class; so the need for a structure seems fairly limited.

If you use the semantic that others and I use that structures should be data only, then any time you see a structure you know that it represents some data and generally is not going to have some rich object model. If you use the semantic of "less clutter" then you have some structures representing data only and some that were more convenient for the developer to use structures for without any other rational reason.

There really aren't a lot of uses for classes with public variables these days. Anything complex enough to need a function or two doesn't sound like a good candidate for public variables.