www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Protected Members in Class

reply Salih Dincer <salihdb hotmail.com> writes:
What do I need to do to see that the protected is active, need a 
separate module?

```d
// Source: https://tour.dlang.org/tour/en/basics/classes
class Any
{
     // protected is just seen by inheriting
     // classes
     protected string type;

     this(string type) {
         this.type = type;
     }

     // public is implicit by the way
     string getType() {
         return type;
     }
}

import std.stdio, std.string;
import std.uni : isWhite;

void main()
{
     Any any = new Any("bu bir deneme");
         any.getType.writeln("--> Split:");
         any.type.split!isWhite.writeln;
         any.type = "deneme";
         any.type.writeln;
}/* Console Out:
bu bir deneme--> Split:
["bu", "bir", "deneme"]
deneme
*/
```
Dec 24 2021
next sibling parent Salih Dincer <salihdb hotmail.com> writes:
On Friday, 24 December 2021 at 08:35:38 UTC, Salih Dincer wrote:
 What do I need to do to see that the protected is active, need 
 a separate module?

 ```d
 // Source: https://tour.dlang.org/tour/en/basics/classes
Addition made for one of the inherited class: ```d override string convertToString() { import std.conv : to; // The swiss army knife of conversion.        this.type = "int";        return to!string(type); } ``` The type is not there but it is changed without super! Why? Thanks...
Dec 24 2021
prev sibling parent reply apz28 <home home.com> writes:
On Friday, 24 December 2021 at 08:35:38 UTC, Salih Dincer wrote:
 What do I need to do to see that the protected is active, need 
 a separate module?

 ```d
 // Source: https://tour.dlang.org/tour/en/basics/classes
 class Any
 {
     // protected is just seen by inheriting
     // classes
     protected string type;

     this(string type) {
         this.type = type;
     }

     // public is implicit by the way
     string getType() {
         return type;
     }
 }

 import std.stdio, std.string;
 import std.uni : isWhite;

 void main()
 {
     Any any = new Any("bu bir deneme");
         any.getType.writeln("--> Split:");
         any.type.split!isWhite.writeln;
         any.type = "deneme";
         any.type.writeln;
 }/* Console Out:
 bu bir deneme--> Split:
 ["bu", "bir", "deneme"]
 deneme
 */
 ```
https://dlang.org/spec/attribute.html#visibility_attributes
Dec 24 2021
parent reply Salih Dincer <salihdb hotmail.com> writes:
On Friday, 24 December 2021 at 10:26:37 UTC, apz28 wrote:
 https://dlang.org/spec/attribute.html#visibility_attributes

Okay, what about the 2nd question (super or this). ```d import app, std.stdio; void main() { auto any = new Any("int"); //any.get().writeln; /* assert(any.data == "int"); /* main.d(7): Error: no property `data` for type `app.Any`, did you mean `app.Any.data`? */ } module app; class Any { protected string data; this(string data) { this.data = data; } property get() { return data; } } ```
Dec 24 2021
parent reply Salih Dincer <salihdb hotmail.com> writes:
On Friday, 24 December 2021 at 11:29:31 UTC, Salih Dincer wrote:
 ```d
 module app;

 class Any
 {
     protected/*
     private//*/
     string Data;

     this(string data) { Data = data; }

      property getData() { return Data; }
 }

 string getData(Any test) { return test.Data; }
```
What is the difference between protected and private? Also how can a function outside of the class access it? S.Dinçer
Dec 24 2021
parent reply Paul Backus <snarwin gmail.com> writes:
On Friday, 24 December 2021 at 12:10:32 UTC, Salih Dincer wrote:
 What is the difference between protected and private?  Also how 
 can a function outside of the class access it?
`private` in D means "this symbol can only be accessed from the module where it's defined." `protected` in D means "this symbol can only be accessed from (a) the module where it's defined, and (b) classes that inherit from the class where it's defined."
Dec 24 2021
parent reply Salih Dincer <salihdb hotmail.com> writes:
On Friday, 24 December 2021 at 14:29:29 UTC, Paul Backus wrote:
 
 `protected` in D means "this symbol can only be accessed from 
 (a) the module where it's defined, and (b) classes that inherit 
 from the class where it's defined."
Please, can you show me this?
Dec 24 2021
parent Paul Backus <snarwin gmail.com> writes:
On Friday, 24 December 2021 at 15:46:17 UTC, Salih Dincer wrote:
 On Friday, 24 December 2021 at 14:29:29 UTC, Paul Backus wrote:
 
 `protected` in D means "this symbol can only be accessed from 
 (a) the module where it's defined, and (b) classes that 
 inherit from the class where it's defined."
Please, can you show me this?
https://run.dlang.io/is/m1IvVr
Dec 24 2021