4 ms·
> 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. Th
by 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.
- mianos 2y agoThe underscore prefix is more about communication. It's not a bad convention as it makes you feel a bit dirty when you are using them outside a class, but, do what you want, we are consenting adults. Whenever I say "I'm no longer attached to all that private stuff", people always reply, "wait until you work on a large code base". I work on a million line+ code base. Whatever. This argument aside, I'm not a total philistine. RAII is awesome but C++ is full to the boot with crusty stuff to keep the compatibility. I always feel there is a language better than anything trying to come out.
- gpderetta 2y agoPython will literally mangle the names of double underscore members by prefixing them with the class name, to make it harder to access from the outside, so it is not just about communication. These days I'm for minimalism, most of my structs are aggregates of public members, but sometimes you really want to make sure to maintain the invariant that your array pointer and your size field are in sync.
- Doxin 2y agoUsing double underscore is advised against, and the name mangling is largely considered a mis-feature these days. Most style guides will tell you to use a single underscore to mark something as not for public consumption. Of course neither double nor single underscore will stop anyone who wants to touch your privates badly enough. Which is big part of the python philosophy: You're not stopped from doing inadvisable things. Instead there's a strong culture around writing "pythonic" code, which largely avoids these pitfalls.
- eru 2y agoAnd neither does C++'s 'private' stop any other code from messing with your data, either, if they want to do that badly enough.
- schmidt_fifty 2y ago[dead]
- steveklabnik 2y ago> 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++. This is not really the case. See https://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B https://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B for a non-exhaustive list. It is true that both sides agree that compatibility is an important goal, but it's only a goal, not something that's 100% the case.