Discuss Class vs. struct. in the C Programming forum on Dev Shed. Class vs. struct. C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
Posts: 1,812
Time spent in forums: 2 Weeks 1 Day 19 h 12 m 24 sec
Reputation Power: 437
Class vs. struct.
In C++, struct and class are synonyms. The only difference is the default access specifier of the members. I can never decide when to type "struct" or when to type "class". The line always seemed so fuzzy to me. I've been at this for years, but when it comes to struct vs class, consistency in my coding style suffers because I can never make up my mind.
Typically, I use struct when I just have a collection of public member data with no member functions. I use class when I have a conceptual object I'd like to represent, or when I start adding some more complex member functions.
When I use struct, as I add more member functions, I find myself stressing the fact that the functions are for "convenience" only -- perhaps to justify my use of "struct" instead of "class". But I really can never decide. It's unfortunate that the "struct" keyword still exists in C++.
"Science is based on reality staying the same, and Nature ignores what humans vote upon." -- Bill Beaty
"Three litres of sherry up the butt can only be described as astounding." -- Darwin Awards
Posts: 1,676
Time spent in forums: 1 Month 3 Days 8 h 23 m 46 sec
Reputation Power: 132
I generally think of a struct for PODS, and a class as something non-POD.
Quote:
Originally Posted by Wikipedia
Plain Old Data Structures in C++ would not contain any member functions and would not use inheritance. They would most naturally be represented as structs.
__________________
Any advertisement triggered by IntelliTxt in this post is not endorsed by the author of this post.
Last edited by Dave Sinkula : July 23rd, 2006 at 01:48 PM.
Posts: 1,812
Time spent in forums: 2 Weeks 1 Day 19 h 12 m 24 sec
Reputation Power: 437
Me too. But then, consider this. You have this struct:
Code:
struct Info {
int a;
char *b;
double c;
}
All is well. But now you want to add some functions. Perhaps a convenient way to initialize all the values, and a convenient way to create a duplicate copy, and I guess a convenient way to free the structure too:
Code:
Info * alloc_info (void);
Info * copy_info (const Info *info);
void free_info (Info *info);
That's all good and well but, you're using C++. If you don't want to stick to C-style coding like that, you may be tempted to do:
Code:
struct Info {
int a;
char *b;
double c;
Info (void);
Info (const Info &);
Info (const Info *);
~Info (void);
}
Generated code is pretty much exactly the same, as is the functionality and design, but the code is arguably easier to read and more importantly, consistent with the classes that you have scattered throughout the rest of the program (if not mixing two styles in the same app is important to you).
So now what? Change it to a class? But it's really just a PODS with the convenient utility functions as members instead of non-members. So maybe it should still be a struct.
Last edited by peenie : July 25th, 2006 at 09:25 PM.
Posts: 1,676
Time spent in forums: 1 Month 3 Days 8 h 23 m 46 sec
Reputation Power: 132
Quote:
Originally Posted by peenie
So now what? Change it to a class? But it's really just a POD with the convenient utility functions as members instead of non-members. So maybe it should still be a struct.
I would likely change it to a struct because it now has member functions so it stopped being POD. I also see a struct in C++ as being something that compiles as a struct in C.
Posts: 6,142
Time spent in forums: 2 Months 2 Weeks 4 Days 33 m 48 sec
Reputation Power: 1974
In practice, I generally agree Dave. If I'm going to use it as I would a struct in C, then I go with a struct. Otherwise, it'll be a class.
However, in my last major C++ project (also my first, back around '93) we discovered on our own that we could add methods to a struct -- we'd never seen it actually discussed in any books, but the ARM suggested that it might be possible, so we gave it a shot and it worked. But the furtherest we would ever take it was to give constructors and destructors to the structs that contained pointers to dynamically created data. I guess that we could have extended that idea to to converting input and output values (eg, store angles internally as radians, but receive input and provide output as degrees, minutes, seconds). Anything more (or even that) and we'd use a class. As it was, we thought we were getting away with something we shouldn't have with those constructors and destructors.
Last edited by dwise1_aol : July 23rd, 2006 at 03:29 PM.
Posts: 1,431
Time spent in forums: 2 Weeks 3 Days 13 h 33 m 57 sec
Reputation Power: 152
for me a struct is just a "simple" hetergenous data type. you have some set of different types that all relate to some logical unit. a class (which is creating a "viable object") woul be needed for being some self contained entity. it can take care of its own members and is a base for inheritance or is inheriting from some base class. its something more, for lack of better words, more OO.
Posts: 452
Time spent in forums: 3 Days 13 h 16 m 32 sec
Reputation Power: 40
I normally use structs when I need all data to be public and there is no inheritance on the data structure.
Else , i will use a class because its clearer to read in the code.
__________________
"Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music."
- Kristin Wilson, Nintendo, Inc., 1989.
Posts: 1,812
Time spent in forums: 2 Weeks 1 Day 19 h 12 m 24 sec
Reputation Power: 437
Quote:
Originally Posted by Andrew80
I normally use structs when I need all data to be public and there is no inheritance on the data structure.
That's pretty much my current attitude as well.
Quote:
Originally Posted by Dave Sinkula
I would likely change it to a struct because it now has member functions so it stopped being POD.
But now you'd have a struct with lots of public members. Granted that's not a problem, but most code that you see that uses classes has accessor functions for all of its members. If I were to change it to a "class", should I then put a layer of abstraction over all the member access just to be consistent? I wouldn't want to do that, but it could easily lead to a weird mix of styles in the same code.
Quote:
I also see a struct in C++ as being something that compiles as a struct in C.
That seems to make sense.
Quote:
Originally Posted by dwise1_aol
However, in my last major C++ project (also my first, back around '93) we discovered on our own that we could add methods to a struct... As it was, we thought we were getting away with something we shouldn't have with those constructors and destructors.
As far as syntax itself goes; that's not really that "sideways". The two keywords are synonyms in C++ (aside from the default access policies). They are handled the same internally as well. And actually, on my version of GCC at least, the following code compiles with no warnings:
Code:
class Whatever {
public:
int _;
};
void function1 (class Whatever *) {
}
void function2 (struct Whatever *) {
}
int main (int, char **) {
Whatever a;
function1(&a);
function2(&a);
return 0;
}
I'm pretty sure the only reason the "struct" keyword is still around at all in C++ is for C compatibility, which, combined with...
Quote:
Originally Posted by X.Cyclop
Struct is part of Structured Programming (C), and Classes is part of Object Oriented Programming (C++, Java, C#).
...is starting to convince me that I won't be using "struct" in C++ ever, for any reason (although it's convenient not having to type "public").
Posts: 365
Time spent in forums: 1 Week 3 Days 9 h 53 m 7 sec
Reputation Power: 147
Ok, I just had to post this since I just learned about it. Did you guys know that there exists a Joint Strike Fighter C++ Coding Standard? Here what it has to say about struct vs. class:
Quote:
4.10.2 Considerations Regarding Access Rights
Roughly two types of classes exist: those that essentially aggregate data and those that provide an abstraction while maintaining a well-defined state or invariant. The following rules provide guidance in this regard.
AV Rule 65
A structure should be used to model an entity that does not require an invariant.
AV Rule 66
A class should be used to model an entity that maintains an invariant.
AV Rule 67
Public and protected data should only be used in structs—not classes.
Rationale: A class is able to maintain its invariant by controlling access to its data. However, a class cannot control access to its members if those members non-private. Hence all data in a class should be private.
Exception: Protected members may be used in a class as long as that class does not participate in a client interface. See AV Rule 88.
Hey, if it's good enough for a military jet fighter, it oughta be good enough for you!
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,390
Time spent in forums: 1 Month 4 Weeks 1 Day 22 h 36 m 15 sec
Reputation Power: 4080
Lots of corporations have coding standards. IIRC, the aviation coding standards were partly based off the ones adopted in the auto industry. I could swear I saw the aviation standards one somewhere a few weeks ago (DDJ perhaps?). One of our members (Clifford perhaps?) alludes to the auto industry's standards of coding, MISRA, quite often.
[edit]Now I remember where I saw the link. Bjarne Stroustrup references it in his FAQ: http://www.research.att.com/~bs/bs_...coding-standard and points to the same document on AT&T's server. Seems he had a hand in describing the coding standard [/edit]
__________________ Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home. "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne
Posts: 1,812
Time spent in forums: 2 Weeks 1 Day 19 h 12 m 24 sec
Reputation Power: 437
Quote:
Originally Posted by KitKat77
Hey, if it's good enough for a military jet fighter, it oughta be good enough for you!
Hmm; that's a nice clear way of looking at it too. Although it still seems like there's that fuzzy gray area where you have an entity with no invariants but convenient operations you can perform on it, where member functions would be the most convenient way to handle it.
Although a constructor/copy constructor/destructor combo on a struct with dynamically allocated data in it would have some constraints placed on it so, I guess in that case, I'm wrong to call the *tors "convenience" functions and it would make sense to use class for that instead.