www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - cannot I override and mark it as private?

reply Jack <jckj33 gmail.com> writes:
I'd to change the visibility of a method overrided from public to 
private but it doesn't work tho to protected it does. Why is that?

give:

```d
class A
{
     void f() { }
}
```

this is ok:

```d
class B : A
{
     protected override void f() { }
}
```

this is not:

```d
class B : A
{
     private override void f() { }
}
```

Why is that? why must I leave it accessible somehow (even if it's 
protected) to all derived class of my derived class?
May 12 2021
next sibling parent Imperatorn <johan_forsberg_86 hotmail.com> writes:
On Wednesday, 12 May 2021 at 19:35:31 UTC, Jack wrote:
 I'd to change the visibility of a method overrided from public 
 to private but it doesn't work tho to protected it does. Why is 
 that?

 give:

 ```d
 class A
 {
     void f() { }
 }
 ```

 this is ok:

 ```d
 class B : A
 {
     protected override void f() { }
 }
 ```

 this is not:

 ```d
 class B : A
 {
     private override void f() { }
 }
 ```

 Why is that? why must I leave it accessible somehow (even if 
 it's protected) to all derived class of my derived class?
Can you begin protected instead? 🤔
May 12 2021
prev sibling parent reply Basile B. <b2.temp gmx.com> writes:
On Wednesday, 12 May 2021 at 19:35:31 UTC, Jack wrote:
 I'd to change the visibility of a method overrided from public 
 to private but it doesn't work tho to protected it does. Why is 
 that?
 ...
 Why is that? why must I leave it accessible somehow (even if 
 it's protected) to all derived class of my derived class?
See specs : https://dlang.org/spec/attribute.html#visibility_attributes
 Symbols with private visibility can only be accessed from 
 within the same module. Private member functions are implicitly 
 final and cannot be overridden.
May 12 2021
parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 5/12/21 5:55 PM, Basile B. wrote:
 On Wednesday, 12 May 2021 at 19:35:31 UTC, Jack wrote:
 I'd to change the visibility of a method overrided from public to 
 private but it doesn't work tho to protected it does. Why is that?
 ...
 Why is that? why must I leave it accessible somehow (even if it's 
 protected) to all derived class of my derived class?
See specs : https://dlang.org/spec/attribute.html#visibility_attributes
 Symbols with private visibility can only be accessed from within the 
 same module. Private member functions are implicitly final and cannot 
 be overridden.
This is (slightly) different, as he's not overriding private member functions. He's overriding public functions, and marking them as private. I think the issue is more that private functions are not allowed to be in the vtable (which is not exactly what the text says). Besides, it would be weird to mark a public function private in a derived type. All that is required is to cast to the base class and then you can call it. I swear I read a bug that discusses this exact issue... -Steve
May 12 2021