www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - protected - a bit too strict or a bug?

reply Arcane Jill <Arcane_member pathlink.com> writes:
Well, there I was, merrily programming away, when I came to the conclusion that
my file had got too big an unmanagable (and this with a team of one person). So
I did the sensible thing and divided it into many files in a nice, sensibly
organize heirarchy, and - no prizes for guessing what - it wouldn't compile.

Curiously though, it wasn't the word "private" that was getting in the way. It
was the word "protected". I wouldn't have expected this, and I'm very surprised.

Here's a cut-down version of the problem:

       class ForwardSecurePRBG : PseudoRandomBitGenerator
       {
           this(PseudoRandomBitGenerator prbg, uint threshold)
           {
               <snip>
               pseudoRandomBitGenerator = prbg;
           }
       
           protected override bit[] getSome(uint numBits)
           {
--PROBLEM----> bit[] r = pseudoRandomBitGenerator.getSome(numBits);
               <snip>
               return r;
           }
       
           private PseudoRandomBitGenerator pseudoRandomBitGenerator;
       }

Now, the compiler's error message, for the line above which I've marked above, was:
       class PseudoRandomBitGenerator member getSome is not accessible

So I have to ask, WHY is it not accessible? As you can see, ForwardSecurePRBG is a subclass of PseudoRandomBitGenerator, so it ought to be able to access any protected member function of PseudoRandomBitGenerator. No? PseudoRandomBitGenerator is now defined in a different module, which is imported. When it was in the same file, everything worked. But the compiler complains. I can only assume it's because it's a protected function of a member variable (pseudoRandomBitGenerator) instead of a protected function of this. But they're the same type. Is this a bug, or have I sorely misunderstood something? I'm sure that would have worked in C++ or Jave. Arcane Jill
Jun 06 2004
parent reply Sean Kelly <sean f4.ca> writes:
Arcane Jill wrote:
 
 Here's a cut-down version of the problem:
 
 
      class ForwardSecurePRBG : PseudoRandomBitGenerator
      {   
          protected override bit[] getSome(uint numBits)
          {
--PROBLEM----> bit[] r = pseudoRandomBitGenerator.getSome(numBits);
              <snip>
              return r;
          }
      
          private PseudoRandomBitGenerator pseudoRandomBitGenerator;
      }

As you can see, ForwardSecurePRBG is a subclass of PseudoRandomBitGenerator, so it ought to be able to access any protected member function of PseudoRandomBitGenerator. No? PseudoRandomBitGenerator is now defined in a different module, which is imported. When it was in the same file, everything worked. But the compiler complains. I can only assume it's because it's a protected function of a member variable (pseudoRandomBitGenerator) instead of a protected function of this. But they're the same type. Is this a bug, or have I sorely misunderstood something? I'm sure that would have worked in C++ or Jave.

This actually wouldn't work in C++, though I don't know about Java. The reason is as you say, because it's a protected function of a member variable. If you've got a copy of the C++ standard, the applicable section is 11.5. Here's a quote: "Except when forming a pointer to member (5.3.1), the access must be through a pointer to, reference to, or object of the derived class itself (or any class derived from that class) (5.2.5)." ie. inheritance only allows access to protected members of that class instance, not all instances of the class or of the parent class. I'll admit that this is somewhat unfortunate as it could be a clever trick to get around access protection, but then that's why it's disallowed :) My guess is that D follows the same rules, even if it's not stated quite so clearly. Sean
Jun 06 2004
parent Antti =?iso-8859-1?Q?Syk=E4ri?= <jsykari gamma.hut.fi> writes:
In article <c9vm8r$2pn1$1 digitaldaemon.com>, Sean Kelly wrote:
 Arcane Jill wrote:
 But the compiler complains. I can only assume it's because it's a protected
 function of a member variable (pseudoRandomBitGenerator) instead of a protected
 function of this. But they're the same type. Is this a bug, or have I sorely
 misunderstood something? I'm sure that would have worked in C++ or Jave.

This actually wouldn't work in C++, though I don't know about Java. [...] ie. inheritance only allows access to protected members of that class instance, not all instances of the class or of the parent class. I'll

...unlike access to private members (which is granted to other objects as well). Welcome to the wonderful world of C++ access rules :) -Antti -- I will not be using Plan 9 in the creation of weapons of mass destruction to be used by nations other than the US.
Jun 06 2004