News for hackers. Stuff that matters.
Re:Public/protected/private is an unnecessary feature by cherryteastain on Thursday June 19, 2025 @03:47PM (#44321911)
I agree that it's unnecessary, and Python's success despite not having private members/fields is testament to that. But public/private fields/methods, like pretty much everything in OOP, is a 'social' rather than engineering innovation. Just as a class tells you certain operations and data are inter-related, private methods or fields signal to a class's users that you probably will break things if you interfere with certain parts.

Otherwise, at least in C++, you can often bypass the private specifier without much difficulty. Perhaps the laziest and easiest way to do so is

    #define private public
    #include "foo.hpp"
    #undef private
Re:Public/protected/private is an unnecessary feature by n2d4 on Thursday June 19, 2025 @04:37PM (#44322318)
Python absolutely does have private members. If a member starts with two underscores, its class name (and an underscore) is prefixed to the member name, essentially resulting in the same behavior as a private modifier. (Although there are ways to circumvent this in Python, there are also ways to access private fields in most other languages.)

But even if that were true, that wouldn't mean that accessibility modifiers are unnecessary in other languages. Python is a certain language with certain idioms, and other languages are more or less OOP and hence some constructs that might not make sense in Python definitely make sense elsewhere.

Re:Public/protected/private is an unnecessary feature by NeutralForest on Thursday June 19, 2025 @04:44PM (#44322380)
I see what you mean what I wouldn't really qualify name mangling as private members, you're discouraged to access them but you can access them all the same.
Re:Public/protected/private is an unnecessary feature by jjice on Thursday June 19, 2025 @05:12PM (#44322578)
I'd consider it the same, personally. It's a social contract that you can absolutely break if you want, just with a bit of work.

Less work in Python than it is Java, but there are ways to get to them in Java, or hell, just modify the source and change it directly. By doing that, you're doing something the developer didn't intend and you shouldn't be surprised if it doesn't work at some point in the future. Same as I'd expect in the Python case.

Re:Public/protected/private is an unnecessary feature by NeutralForest on Thursday June 19, 2025 @05:30PM (#44322706)
Sure, I think the bar to modify is higher in Java for example but I can't really find fault in what you're saying because I'd never unknowingly modify a member with double underscores in Python.
Re:Public/protected/private is an unnecessary feature by throwaway2037 on Friday June 20, 2025 @01:00AM (#44324873)

    > but you can access them all the same
Can't you do the same in C++ using struct offsets and reinterpret_cast<>? I'm not sure what you are saying here.
Re:Public/protected/private is an unnecessary feature by NeutralForest on Friday June 20, 2025 @03:34AM (#44325494)
I'm not familiar with C++ enough to know about circumventing `private` but in Python you can access "private" members just like any other "public" ones, with the dot notation. There's no specific enforcement at the language level, no error will be thrown.
Re:Public/protected/private is an unnecessary feature by colejohnson66 on Friday June 20, 2025 @08:29AM (#44327049)
Something something undefined behavior
Re:Public/protected/private is an unnecessary feature by TylerE on Thursday June 19, 2025 @08:50PM (#44323855)
That is absolutely a social construct and not a language feature. You can override “private” members just fine with a simple assignment.
Re:Public/protected/private is an unnecessary feature by _ZeD_ on Friday June 20, 2025 @12:17AM (#44324705)
No, it doesn't: double underscore field names are mangled basically to avoid easy overriding in su classes; keep in mind that it is a feature that predates super() in almost any form
Re:Public/protected/private is an unnecessary feature by masklinn on Friday June 20, 2025 @01:20AM (#44324953)
Not only are those not technically private, they were never intended for information hiding in the first place. Name mangling was introduced to avoid namespace conflicts in inheritance scenarios.
Re:Public/protected/private is an unnecessary feature by jjmarr on Thursday June 19, 2025 @05:18PM (#44322610)
You should safely shoot yourself in the foot with `friend`, which is a core language feature for accessing private members/functions/etc.

https://www.cppreference.com/w/cpp/language/friend.html

Redefining public as private can violate the ODR rule and triggers undefined behaviour because private members are ordered in memory in an undefined way, while public members are ordered in a standard way.

Re:Public/protected/private is an unnecessary feature by leni536 on Thursday June 19, 2025 @05:19PM (#44322615)
Don't do that in C++. Maybe unless if you are debugging. This can blow up in your face in unexpected ways.
Re:Public/protected/private is an unnecessary feature by WalterBright on Thursday June 19, 2025 @08:16PM (#44323716)
#define duck_season wabbit_season

#include "IDemandYouShootMeNow.hpp"

Re:Public/protected/private is an unnecessary feature by ethan_smith on Thursday June 19, 2025 @10:17PM (#44324178)
The #define private public hack doesn't work reliably in modern C++ compilers and violates the One Definition Rule, potentially causing undefined behavior and hard-to-debug errors.
Re:Public/protected/private is an unnecessary feature by CodeHz on Friday June 20, 2025 @12:46AM (#44324813)
Actually, it won't compile in msvc (the linking phase, to be precise), MSVC uses different names for methods with different visibilities
Re:Public/protected/private is an unnecessary feature by jmmv on Friday June 20, 2025 @01:13AM (#44324927)
I’ve seen a ton of this “#define private public” in a previous team that was forced to write unit tests in a rush due to the whims of leadership. It hurt to see, and the resulting white box tests were useless.
Re:Public/protected/private is an unnecessary feature by colanderman on Friday June 20, 2025 @01:28PM (#44329923)
That #define trick is not valid due to how C++ groups members of different visibility classes together. See e.g. https://stackoverflow.com/a/36149568 It will work until it doesn't.