6 ms·
In other words, C structs and C++ structs are not the same thing (although C++ can usually handle C structs too). C structs are Plain Old Data. C++ structs are
by gary_0 2y ago
In other words, C structs and C++ structs are not the same thing (although C++ can usually handle C structs too). C structs are Plain Old Data. C++ structs are "objects" and there are pages and pages of rules in the C++ Standard about what that means, and there's no way the C Standard can incorporate all that. And you can't drag any C++ struct/class features into C without dragging in all those rules with them.
- stonemetal12 2y agoYep. The only difference between struct and class in C++ is that class defaults to private while struct defaults to public. 100% of the using structs like they are C structs vs using class as objects is cultural not a part of the language.
- SAHChandler 2y agoThe public vs. private aspect also affects inheritance. structs publicly inherit from base types by default, classes privately inherit from base types.
- gary_0 2y agoAny similarly of keyword naming between C and C++ is purely coincidental. :P C++ is somewhat unique in that it started out as a few extra features on top of C before gradually splitting off and mutating into a totally separate programming language.
- pjmlp 2y ago"Unique" in a world where Objective-C, Objective-C++, Groovy, and TypeScript exist.
- gary_0 2y agoWhat I meant wasn't that it was a language that was compatible with an earlier language. Groovy just compiles to the JVM; lots of things do. TypeScript is just JavaScript with type safety; Python did that too. Objective-C was just NeXT attempting to make the ugliest-looking programming language possible and they succeeded immediately. But Cfront was released circa 1983 and you basically just wrote C, but it added a bit of new syntax that generated extra C behind the scenes. Object-oriented programming was still fetal in 1983! It didn't get really hyped until the mid-90's. So C++ kind of mutated for decades as this gross appendage on C until it became this whole separate blob that ate half of programming. It was 15 years later when the C++98 "standard" started trying to reign in Dr. Stroustrup's monster. Then in 2005 we threw away all our textbooks that were like "Look! `Apple` derives from `Fruit`! `Car` derives from `Engine`! This is going to change the world!" because adding object-orientedness to everything became uncool when our bosses became fans of Java. But by this point the C++ blob had taken on a life of its own... So yeah. Very few programming languages have a story as long and insane as C++.
- pjmlp 2y agoObjective-C was originally a macro processor just like CFront on top of C. Objective-C++ likewise on top of CFront. Until like with CFront, they became selfhosted compilers. Groovy code is Java code, regardless of targeting the JVM, the same syntax is supported and extended with dynamic capabilities. Object Pascal was created for Lisa project, exactly in 1983. Tom Love and Brad Cox created Objective-C in 1984.
- kazinator 2y agoSupport for POD (plain old datatype) structs in C++ is definitely part of the language.
- chipdart 2y ago> 100% of the using structs like they are C structs vs using class as objects is cultural not a part of the language. I think this take is completely wrong. There is nothing cultural about it. C++ was created as a strict superset of C, and thus from the inception it supported all features made available in C. This design goal remains true up to this day, and only started to diverge relatively recently when C was updated to include features that were not supported (yet) by C++. When someone declares a plain old struct in C++, they are declaring a struct that is perfectly compatible and interoperable with C. This is by design. From the inception.
- mianos 2y agoWhat if, after 20 years of C++, you spend 10 years doing python, only to go back to C++ and realise that all this private/protected stuff is a crock and most of the time you are doing real work you just use struct and start typing your C++, virtual functions, constructors, destructors etc? Just asking, for a friend.
- josefx 2y ago> you spend 10 years doing python, only to go back to C++ and realise that all this private/protected stuff is a crock Just a friendly reminder that two leading underscores wont protect your member functions in C++. Even if people insist that those are totally not supposed to be private in python.
- schmidt_fifty 2y ago[dead]
- nhatbui 2y agoI think OP meant discarding public/private constructs entirely, no protection, like in python.
- gpderetta 2y agoExcept python started mangling double underscore in a futile attempt to implement private members/methods.
- chipdart 2y ago> In other words, C structs and C++ structs are not the same thing (although C++ can usually handle C structs too). C structs are Plain Old Data. C++ structs are "objects" and there are pages and pages of rules in the C++ Standard about what that means, and there's no way the C Standard can incorporate all that. I think this glances over what structs actually are in C++, and unwittingly portrays them as something different. Structs in C++ are definitely exact like structs in C. Or they can be, if that's what you're aiming for. If you include a C header file that defines a struct in a C++ program, you build it, and you use instances of that struct to pass them to C programs, everthing just works. The detail you need to be mindful of is that C structs support a subset of all the features supported by C++ classes, and once you start to use those features C++ also allows implementations to forego some constraints. If you expect to use a struct in C++ but still define it in a way that you make it include features that are not supported in C then you can't pin that on the language. https://learn.microsoft.com/en-us/cpp/cpp/trivial-standard-layout-and-pod-types https://learn.microsoft.com/en-us/cpp/cpp/trivial-standard-l... Using C-like structs is a very common use case, to the point that the standard explicitly defines the concept of standard layout and builds upon that to specify the concept of a standard layout type. A struct/class that is a standard layout type, which means it's a POD type, corresponds exactly with C structs. They are explicitly defined in terms of retaining interoperability with other languages.
- schmidt_fifty 2y agoThe detail missing from this explanation is that structs and classes are the same thing with different default visibility. I found this enormously confusing when learning the language, and I think it was a major mistake. My assumption was that a struct was exactly the same as a c struct, and the "new" functionality was all a part of the classes. Still. There's always extern "c".
- rramadass 2y agoExactly. This is one of the basic things (i.e. PODs) you learn in C++ that i am surprised the gp didn't know of it. I myself have written systems with C-structs/C-api and wrapped the same C-structs (by deriving) in C++ classes (being careful with any introduced vptr/vtable) to happily provide/extend C-code/libraries via C++ apis.
- KingLancelot 2y ago[dead]