luiscubal
|
|
Posted on: June 30, 2011, 07:15:37 pm |
|
|
Joined: Jun 2009
Posts: 452
|
There are several things I hate in programming, such as regular expressions, segmentation faults, those annoying Swing exceptions that pop once in a while out of nowhere and contain only Swing/AWT functions in the stack trace, massive compile times, GCC C++ template error messages, the lack of a standard UI library in .NET, Java's pseudo-generics, the loading times of slow IDEs and the protected access modifier.
Today, I feel like insulting protected to hell. Why? Because it's worthless. Because I hate it almost as much as I hate regular expressions, and because I apparently have nothing better to do with my time. It's worthless in C++(I'm talking about fields/methods, I haven't thought much about "class foo : protected bar" so I won't comment on that). It's worthless in Java. It's worthless in C#. It's worthless in pretty much any language.
Seriously, we can classify classes in two categories: Those that can be inherited, and those that can not. Often languages provide special keywords(such as "sealed" and "final") to prevent a class from being inherited. Even if that is not used, private constructors can often put a stop to inheritance.
Protected is worthless in both types of classes.
Let's assume field(or method) Foo is declared in class Bar as protected. Now class Bar can either be inherited in a given context or it can not.
If, in that context, Bar can't be inherited, then Foo is effectively private(or, at most, the equivalent of C#'s "internal", which is a much better approach to this problem).
If, in that context, Bar can be inherited, then one can create a dummy class BarExtension with only dummy constructors, dummy implementations of abstract methods, and functions declared like "TypeOfFoo GetFoo(Bar* bar)" and "void SetFoo(Bar* bar, TypeOfFoo foo)". These functions can possibly be static(or not, whatever, both would work). Since now one can modify the Foo of ANY Bar with the help of BarExtension(which can be defined/declared pretty much anywhere), this means that Foo is effectively "public".
So, in any given context, Foo is either private or public. It just gives a false sensation of security, without solving anything.
Just wanted to share this. Feel free to move along with your lives now.
|
|
|
Logged
|
|
|
|
|
|
|
|
|
|
|
|
|
Josh @ Dreamland
|
|
Reply #10 Posted on: July 02, 2011, 10:50:52 am |
|
|
Prince of all Goldfish
Location: Pittsburgh, PA, USA Joined: Feb 2008
Posts: 2950
|
Besides, it doesn't matter if C++ offers a useless keyword here and there; we all know C++ is indisputably the best language known the man, if not the best language of all time.
|
|
|
Logged
|
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble "I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
|
|
|
|
|
|
|