2010-05-01 14:20:27 8 Comments
After finishing my C++ class it seemed to me the structs/classes are virtually identical except with a few minor differences.
I've never programmed in C before; but I do know that it has structs. In C is it possible to inherit other structs and set a modifier of public/private?
If you can do this in regular C why in the world do we need C++? What makes classes different from a struct?
Related Questions
Sponsored Content
34 Answered Questions
23 Answered Questions
[SOLVED] Why do we need virtual functions in C++?
- 2010-03-06 07:10:35
- Jake Wilson
- 472944 View
- 1065 Score
- 23 Answer
- Tags: c++ virtual-functions
21 Answered Questions
[SOLVED] What is the "-->" operator in C++?
- 2009-10-29 06:57:45
- GManNickG
- 664319 View
- 8110 Score
- 21 Answer
- Tags: c++ operators code-formatting standards-compliance
1 Answered Questions
17 Answered Questions
[SOLVED] Are static class variables possible?
- 2008-09-16 01:46:36
- Andrew Walker
- 1032757 View
- 1643 Score
- 17 Answer
- Tags: python class static class-variables
28 Answered Questions
6 Answered Questions
[SOLVED] C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?
- 2011-06-11 23:30:14
- Nawaz
- 181451 View
- 1676 Score
- 6 Answer
- Tags: c++ multithreading c++11 language-lawyer memory-model
6 comments
@Antal Spector-Zabusky 2010-05-01 14:28:22
In C++, structs and classes are pretty much the same; the only difference is that where access modifiers (for member variables, methods, and base classes) in classes default to private, access modifiers in structs default to public.
However, in C, a struct is just an aggregate collection of (public) data, and has no other class-like features: no methods, no constructor, no base classes, etc. Although C++ inherited the keyword, it extended the semantics. (This, however, is why things default to public in structs—a struct written like a C struct behaves like one.)
While it's possible to fake some OOP in C—for instance, defining functions which all take a pointer to a struct as their first parameter, or occasionally coercing structs with the same first few fields to be "sub/superclasses"—it's always sort of bolted on, and isn't really part of the language.
@Abhijeet 2015-12-13 14:05:13
From OOP prospective .Net guys have defined it this way ✓ CONSIDER defining a struct instead of a class if instances of the type are small and commonly short-lived or are commonly embedded in other objects. X AVOID defining a struct unless the type has all of the following characteristics: 1. It logically represents a single value, similar to primitive types (int, double, etc.). 2. It has an instance size under 16 bytes. 3. It is immutable.
@Antal Spector-Zabusky 2015-12-21 06:08:44
@Abhijeet That's the distinction between structs and classes in C#, but that's simply irrelevant to C++, and even more so to C. In C#, classes and structs are actually different; not so in C++, and C only has structs without OO.
@Dave 2016-10-14 16:45:43
I'm going to add to the existing answers because modern C++ is now a thing and official Core Guidelines have been created to help with questions such as these.
Here's a relevant section from the guidelines:
The code examples given:
Class
es work well for members that are, for example, derived from each other or interrelated. They can also help with sanity checking upon instantiation.Struct
s work well for having "bags of data", where nothing special is really going on but the members logically make sense being grouped together.From this, it makes sense that
class
es exist to support encapsulation and other related coding concepts, thatstruct
s are simply not very useful for.@Dave 2016-10-14 16:58:00
One other consideration is portability.
struct
s are the most portable. They can be used by C or C++ or back and forth. They can also be unpacked in Python using thestruct
module, for example. If your project prioritizes being compatible with other languages, interfaces or systems, preferstruct
overclass
. For strictly program-internal affairs, preferclass
.@MarsRover 2012-09-12 12:07:52
One more difference in C++, when you inherit a class from struct without any access specifier, it become public inheritance where as in case of class it's private inheritance.
@Lance Diduck 2010-05-01 15:08:18
Other that the differences in the default access (public/private), there is no difference.
However, some shops that code in C and C++ will use "class/struct" to indicate that which can be used in C and C++ (struct) and which are C++ only (class). In other words, in this style all structs must work with C and C++. This is kind of why there was a difference in the first place long ago, back when C++ was still known as "C with Classes."
Note that C unions work with C++, but not the other way around. For example
And likewise
only works in C
@Dani 2016-04-16 22:36:13
Using cpp keywords in your c code and then claiming it is not cpp compatible is rather stupid
@Chris Hafey 2010-05-01 14:29:42
C++ uses structs primarily for 1) backwards compatibility with C and 2) POD types. C structs do not have methods, inheritance or visibility.
@Billy ONeal 2010-05-01 14:32:10
For what it's worth, most STL functors (i.e.
std::less
) are defined as structs, not classes.@robertwb 2015-01-03 04:18:38
Note that C++ structs do have methods, inheritance, and visibility.
@Error 2018-01-08 04:14:24
c struct can include function pointer though as type(*addr)(params);
@Johannes Schaub - litb 2010-05-01 14:22:02
It's not possible to define member functions or derive structs from each other in C.
Also, C++ is not only C + "derive structs". Templates, references, user defined namespaces and operator overloading all do not exist in C.
@anon235370 2010-05-01 14:23:23
I know that the templates, etc to do not exist in C but I was not aware of the
power
of structs in C. So then C++ only uses structs to be 'backwards' compatible with C?@dmckee 2010-05-01 14:26:03
Just for backwards compatibility? On a practical basis there is probably something to that, but the distinction can be a signal of intent: where I use a
struct
I mean a largely passive POD type of thingy.@Billy ONeal 2010-05-01 14:30:37
@dmckee: For what it's worth, most STL functors (i.e.
std::less
) are defined as structs, not classes.@ypnos 2010-05-01 14:30:40
C++ is not fully backards compatible with C. You could say that the struct keyword is an accommodation to C developers. I like the struct keyword for classes that merely hold data in an ordered fashion but not provide (much) logic themselves.
@Billy ONeal 2010-05-01 14:31:32
@ypnos: See my last comment. The only difference between the two is that one's members are default public, and the other are default private.
@ypnos 2010-05-01 14:32:36
@Billy ONeal: Well, you could argue that STL functors are another implementation of a function pointer, which, essentially, is data ;-)) And I know of the single difference.