www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - protected behaviour on base class member access from another module

reply Boris-Barboris <ismailsiege gmail.com> writes:
Hi!

I have a base class in module A:

module A;
...
class GuiElement: GuiComponent
{
     protected
     {
         GuiElement _parent;
...
}

template isGuiElement(T)
{
     enum isGuiElement = is(T: GuiElement);
}
...


and derived class in module B:

module B;
...
class Div(uint dim, uint odim): GuiElement
{
     protected GuiElement[] children;

     this(Children...)(GuiManager manager, Children kids)
	if (allSatisfy!(isGuiElement, Children))
     {
	super(manager);
	children = [kids];
         this._parent = null;           <- This works
	foreach (kid; children)
	    kid._parent = this;        <- Error: class A.GuiElement 
member _parent is not accessible


A couple of questions:
1). Is this intended?
2). If yes, why? What is the reasoning behind such restriction? 
Quoting the docs: "...a symbol can only be seen by members of the 
same module, or by a derived class...".
3). What does the cryptic "If accessing a protected instance 
member through a derived class member function, that member can 
only be accessed for the object instance which can be implicitly 
cast to the same type as ‘this’" mean?
     Let's say B derives from A, and B instance has a method that 
accesses A.protected_member inside of it. What does member access 
have to do with it? Why does this sentence jump from member to 
method, back to member, and then to some object that was never 
mentioned before? Objects don't request access, statements do. 
`this` of who?
Apr 30
parent Boris-Barboris <ismailsiege gmail.com> writes:
Ok, sorry, look's like that was always the case in C++, so it's 
too late to question it. I'll just elevate it to package, I guess.
Apr 30