July 15, 2006 Re: Private visible? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | Walter Bright wrote:
> Lucas Goss wrote:
>> Walter Bright wrote:
>>>
>>> Sometimes it is valuable to be able to say "you can't do this operation with this type". I don't see why this ability should be restricted to C++.
>>
>> Can someone come up with an example or explain why this would be valuable? To make sure we're on the same page, we're talking about:
>>
>> ----
>> class Base {
>> public:
>> virtual int Number() = 0;
>> };
>> ...
>> class Child : public Base {
>> private:
>> int Number() { return 1; }
>> };
>
> I think it's a bug that the compiler allows this. It should be illegal code, because it breaks encapsulation.
Um, maybe I didn't make it clear enough, or maybe I'm not understanding. The above is C++ code that went with this code:
(1)
Child c;
int num = c.Number(); //error: cannot access private member
(2)
Child* cp = new Child();
num = cp->Number(); //error: cannot access private member
(3)
Base* bp = new Child();
num = bp->Number(); // num = 1
So the compiler does error with the first two attempts, but the third compiles and runs fine. I was just trying to find an example of 'poisoning' a base class that you mentioned. To me it doesn't sound like very good practice (maybe that's why C# and I think Java disallow such practice). I just wanted to make sure I'm understanding correctly.
Lucas
|
July 15, 2006 Re: Private visible? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lucas Goss | Lucas Goss wrote:
> So the compiler does error with the first two attempts, but the third compiles and runs fine.
Yes, it is C++ and is perfectly legal C++. A more interesting question is should it be legal? I posed the question in comp.lang.c++.moderated, and got a couple of interesting answers.
|
July 16, 2006 Re: Private visible? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | Walter Bright wrote:
> Lucas Goss wrote:
>
>> So the compiler does error with the first two attempts, but the third compiles and runs fine.
>
>
> Yes, it is C++ and is perfectly legal C++. A more interesting question is should it be legal? I posed the question in comp.lang.c++.moderated, and got a couple of interesting answers.
in Java you cannot override a method with a more protected access. I consider it a safeguard, so that I do not get caught in a lattice of protection attributes and inheritance.
-DavidM
|
July 16, 2006 Re: Private visible? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lucas Goss | Lucas Goss wrote: > Bruno Medeiros wrote: >> >> Good. That should be documented in the spec then, that isn't mentioned anywhere, as far as we know. >> > > Uh, that was C++ code, sorry I didn't make it clear. > > Lucas Yes, but what Walter said (about the spec) was referring to D, even if your post was a C++ code. -- Bruno Medeiros - CS/E student http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D |
July 16, 2006 Re: Private visible? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | Walter Bright wrote: > Lucas Goss wrote: >> So the compiler does error with the first two attempts, but the third compiles and runs fine. > > Yes, it is C++ and is perfectly legal C++. A more interesting question is should it be legal? I posed the question in comp.lang.c++.moderated, and got a couple of interesting answers. Could you post the link to that discussion? (I don't think I can easily find it myself) ( Also, if you don't know already, C# adopts an invariant behavior: it disallows to change the protection in any way. Java has a covariant behavior, as mentioned by David. ) -- Bruno Medeiros - CS/E student http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D |
July 16, 2006 Re: Private visible? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | Walter Bright wrote: > Lucas Goss wrote: >> So the compiler does error with the first two attempts, but the third compiles and runs fine. > > Yes, it is C++ and is perfectly legal C++. A more interesting question is should it be legal? I posed the question in comp.lang.c++.moderated, and got a couple of interesting answers. That's what I'm trying to discover, is there a good reason it should be legal. I found the thread here (for those interested). http://groups.google.com/group/comp.lang.c++.moderated/browse_thread/thread/38a8bf39637180de/# Lucas |
July 17, 2006 Re: Private visible? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bruno Medeiros | Bruno Medeiros wrote:
> Walter Bright wrote:
>
>> Lucas Goss wrote:
>>
>>> So the compiler does error with the first two attempts, but the third compiles and runs fine.
>>
>>
>> Yes, it is C++ and is perfectly legal C++. A more interesting question is should it be legal? I posed the question in comp.lang.c++.moderated, and got a couple of interesting answers.
>
>
> Could you post the link to that discussion? (I don't think I can easily find it myself)
>
> ( Also, if you don't know already, C# adopts an invariant behavior: it disallows to change the protection in any way. Java has a covariant behavior, as mentioned by David. )
>
Not to mention you can always throw an Exception in the disallowed operations, so I don't see this as a great usage of protection attributes.
-DavidM
|
July 17, 2006 Re: Private visible? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bruno Medeiros | Bruno Medeiros wrote:
> Could you post the link to that discussion? (I don't think I can easily find it myself)
It's entitled:
Restricting access should be illegal?
posted 7/15/2006 3:38 AM on comp.lang.c++.moderated.
It's an ongoing discussion, so it'd be premature to summarize here.
|
Copyright © 1999-2021 by the D Language Foundation