how to change attribute in descendant?

Jari-Matti Mäkelä jmjmak at utu.fi.invalid
Thu Jan 18 09:19:47 PST 2007


Pragma kirjoitti:
> Jari-Matti Mäkelä wrote:
>> Eric, do you happen to know, if it's possible to do the opposite - i.e.
>> make public methods private by using private/protected inheritance? The
>> spec says (http://www.digitalmars.com/d/class.html):
>>
>> InterfaceClass:
>>         Identifier
>>         Protection Identifier
>>
>> Protection:
>>         private
>>         package
>>         public
>>         export
>>
>> But what does it mean? I've asked this a few times before (nobody
>> answered). The compiler just bypasses the protection attributes. I think
>> other than public inheritance is anyway bad in D, especially when the
>> protection is used with interfaces.
> 
> (I'm assuming that you're coming from a novice background here - my
> apologies if this is not the case)

Sorry - the question was a bit naive. What I meant was that why isn't it
working.

> 
> ...where a scope is a module, interface, class or struct.
> 
> Well one thing that isn't illustrated in the grammar (quoted above) is
> that 'public' is implied for anything accessed from within the same
> module

Yes.

>- perhaps this is what you're seeing when the compiler 'bypasses'
> things?

No. You see, if I read and quess the grammar correctly, some of this
should work:

module aaa;

interface A {
 public void a(); // the public is of course implied here
}

class B {
 public void b() { }
}

---

module bbb

class C :
private A, private B // the problem is this part of the grammar
{
 private void a() { }
}

---

module ccc

void main() {
 auto a = new C();
 a.a(); // should raise a compile time error ?
 a.b(); // should raise a compile time error
}

---

Here's the same code in standard C++ and it correctly raises a compile
time error:

#include <stdio.h>

class A {
 public:
 void a() { printf("hello"); }
};

class B: private A {
};

int main() {
        B *b = new B();
        b->a();
        return 0;
}

> You have to break your classes and interfaces out into distinct
> files for these attributes to be enforced as you'd expect.

But it doesn't help.


More information about the Digitalmars-d-learn mailing list