Can someone please explain to me why exposing your private state and implementation functions in c++ header files is encouraged? I'm not talking about mutability, but rather allowing other implementation files to see and be physically coupled to "private" implementation details exposed through the header e.g. (member datatypes and names). Is it just me who has a serious problem with this?
The only alternative i'm aware of is the pimpl idiom, which is definitely not an ideal solution. While it does give you complete encapsulation, it results in boilerplate code, and a runtime performance hit, which is ludicrous..
What kind of mental gymnastics do you have to do in order to convince yourself that private: is private?
pic related is the only book I've seen really address this issue
Sounds like a consequence of supporting multiple inheritance. C++ is pretty yikes in general tho, so it’s unsurprising.
Benjamin Wright
agreed. I find myself jumping back and forth between c and c++, but ultimately ending up at C every time because of shit like this
Cooper Bell
>c++ thats a polymorphic virtual inherited templated boosted yikes from me
Evan Howard
I have a tmp for that
Jason Gutierrez
>encouraged Your biggest problem OP is that you are pretending cpp is object oriented language. Hint: its not. Cpp is wild west of languages where everything is possible.
The fact that something is possible or can gain you 0.1% faster code doesnt automatically mean you should use it. In almost all cases readibility of code is always higher on priority ladder than raw speed.
Dont give a damn about why its common. Pretend it isnt a thing and feel free to keep your private atributes and methods hidden.
Lincoln King
but that's how c++ is marketed and taught.
I agree to an extent with your point that writing readable code trumps most other things, however, it also depends on your target platform and performance characteristics. As a core game engine developer, it's simply not acceptable to use something like the pimpl idom in the middle of a render tick, iterating through the visible set.
I also agree with your last point, however generally you have to work with other people who perhaps do not have that same view point and have constrained themselves to that OOP paradigm, and have been taught c++ by putting all of their "private" members in the header file, because that's the supposed standard c++ way to do things.
What i'm trying to do is get people to question whether they should be exposing this state and what the ramifications of doing that is. The usual answer i get is: "oh well it's private, no one can access it" which completely misses the point
Isaac Robinson
From the average programmer's point of view, it doesn't matter. The point of private state/functions is to keep the intern from fucking with something they aren't supposed to, or to catch your own errors before it compiles. The features aren't meant to provide true security.
David Thompson
Okay. One way why people put private stuff into header is because some (maybe all?) compilers otherwise cant do inlines and other optimalisations. Which doesnt make sense if the compiler has access both to cpp and h file, but in some cases (DLL, static libraries) the compiler has access only to the header file.
And btw if you are writing such exotic stuff as 3D game engine (basicaly anything like pipeline), almost all OOP concepts has to go out of window because they are too slow. Many uni profesors will tell you that using GOTO is bad, but then they never even wrote html parser. This is similiar issue: theory VS practical world.
And he is correct, almost all fancy keywords like private, public in cpp are mostly for static / compile check. You can do literally anything in runtime with cpp. Nothing (often not even OS or virtulisation) can stop you from reading memory. Sometimes you can even read memory you didnt alocated which is the basic doom of all modern container based frameworks which promises "security".
Charles Cook
based sepples dun give a fuk
Jackson Davis
right, but you can do the same with even more encapsulation by keeping the private state in the implementation file and only exposing the functions that operate on that state as part of the api. There's no benefit to using a class here.
Certainly, there are cases where things do need to go in the header file, but they should be well thought out and not dumped in there because you needed another variable in your implementation code for example.
As a side note: compilers actually can and do inline functions in implementation files providing they are kept short. There is also link time optimisation as a further level, but that's going off topic.
Thomas Flores
The standard approach is the easiest, and there's no real downside to it. If you want actual code security then that's a completely different topic.
Angel Lewis
how is it the easiest? it's just adding more code for less of the benefit. You also have to think about construction, destruction, copy assignment, and the list goes on.
There's multiple downsides. To name a few: 1. Every time you add a private member or function, everyone who includes that header file has to be recompiled, which for a large project, can be a very big deal.
2. Makes it easier for people to make assumptions about how your implementation works and potentially make it hard for you to change said implementation in the future.
Nathaniel Lee
Sounds like nerd shit to me
Luis Fisher
It's not like you can just ignore the Big 3 by moving where you have your code. Yes, changes can break compatibility, although with any change you have to do some recompilation/distribution.
Austin Morgan
Can't you make binary header files? Aren't the standard library headers binary files?
Ian Sullivan
C++20 fixes this with modules. You still use header files inside of a module to manage type interdependencies, but anyone consuming your module doesn't need to know about the header files.
Samuel James
just dont use oop and you are good to go how hard is it to understand?
Isaac Smith
>what is pimpl idiom
Levi Ross
Same thing goes for any change in a header dude not just private variables. We don't understand what your point is.
Angel Carter
What exactly is the problem here? That you can see the name of variables in a header? What kind of problem are you trying to solve?
Jack Sullivan
An old gay practice to speed up compilation popular before distributed compilers and faster machines which makes fuckass shit code
Liam Flores
how are you supposed to know the size of objects without having their description? dumbass.