www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Coderiving

reply Karen Lanrap <karen digitaldaemon.com> writes:
Why one can derive from a nested class only in the corresponding 
super class?

The example gives the error:
 super class Id is nested within Concept, not Concrete

But Concrete is derived from Concept and should contain the definition of the class Id! <code> interface Identifier { char[] whoAmI(); } class Concept { class Id:Identifier { char[] whoAmI() { return "Concept.".dup; } } } class Concrete: Concept { class Idl: Id //does not work { char[] whoAmI(){ return "Concrete.".dup; } } } void main() { auto instance= new Concrete; } </code>
Nov 21 2006
next sibling parent reply Sean Kelly <sean f4.ca> writes:
Karen Lanrap wrote:
 Why one can derive from a nested class only in the corresponding 
 super class?
 
 The example gives the error:
 super class Id is nested within Concept, not Concrete

But Concrete is derived from Concept and should contain the definition of the class Id!

I think this is because inner classes can access data in the private scope of their surrounding class, and private class data should not be accessible by derived classes. Sean
Nov 21 2006
parent reply BCS <BCS pathilink.com> writes:
Sean Kelly wrote:
 Karen Lanrap wrote:
 Why one can derive from a nested class only in the corresponding super 
 class?

 The example gives the error:
 super class Id is nested within Concept, not Concrete

But Concrete is derived from Concept and should contain the definition of the class Id!

I think this is because inner classes can access data in the private scope of their surrounding class, and private class data should not be accessible by derived classes. Sean

Based on that argument, this shouldn't work. <code file="a.d"> private int a; class Foo { void fig(){a++;} } </code> <code file="b.d"> class Bar : Foo { void bar(){fig():} } </code> The derived nested class would have no more access than any other code. Yes it could access protected stuff by way of of inherited members but that is the way things are supposed to work.
Nov 21 2006
parent Sean Kelly <sean f4.ca> writes:
BCS wrote:
 Sean Kelly wrote:
 Karen Lanrap wrote:
 Why one can derive from a nested class only in the corresponding 
 super class?

 The example gives the error:
 super class Id is nested within Concept, not Concrete

But Concrete is derived from Concept and should contain the definition of the class Id!

I think this is because inner classes can access data in the private scope of their surrounding class, and private class data should not be accessible by derived classes. Sean

Based on that argument, this shouldn't work. <code file="a.d"> private int a; class Foo { void fig(){a++;} } </code> <code file="b.d"> class Bar : Foo { void bar(){fig():} } </code> The derived nested class would have no more access than any other code. Yes it could access protected stuff by way of of inherited members but that is the way things are supposed to work.

I considered this before posting, and perhaps you're right. Maybe this is just a technical issue, as I suspect the 'outer' pointer is handled differently than how parent class data is accessed. Sean
Nov 21 2006
prev sibling next sibling parent reply "JohnC" <johnch_atms hotmail.com> writes:
"Karen Lanrap" <karen digitaldaemon.com> wrote in message 
news:Xns9882A8C0ABB63digitaldaemoncom 63.105.9.61...
 Why one can derive from a nested class only in the corresponding
 super class?

 The example gives the error:
 super class Id is nested within Concept, not Concrete

But Concrete is derived from Concept and should contain the definition of the class Id! <code> interface Identifier { char[] whoAmI(); } class Concept { class Id:Identifier { char[] whoAmI() { return "Concept.".dup; } } } class Concrete: Concept { class Idl: Id //does not work { char[] whoAmI(){ return "Concrete.".dup; } } } void main() { auto instance= new Concrete; } </code>

To make it work, declare Id and Idl with the static attribute. class Concept { static class Id : Identifier { ... } } class Concrete : Concept { static class Idl : Id { ... } }
Nov 21 2006
parent Karen Lanrap <karen digitaldaemon.com> writes:
JohnC wrote:

 To make it work, declare Id and Idl with the static attribute.

Yes this works in this case, but only because Id does not access any outer information?
Nov 21 2006
prev sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Karen Lanrap" <karen digitaldaemon.com> wrote in message 
news:Xns9882A8C0ABB63digitaldaemoncom 63.105.9.61...
 Why one can derive from a nested class only in the corresponding
 super class?

 The example gives the error:
 super class Id is nested within Concept, not Concrete

But Concrete is derived from Concept and should contain the definition of the class Id!

Walter's response when I asked for this was "AAAAAAIEEEEE!!!". He said basically that it's kind of a niche case, and that unless there was a truly demonstrable need for it, he wouldn't try to implement it.
Nov 21 2006
parent reply Karen Lanrap <karen digitaldaemon.com> writes:
Jarrett Billingsley wrote:

 He said basically that it's kind of a niche case, and that
 unless there was a truly demonstrable need for it, he wouldn't
 try to implement it. 

That is the usual type of argument ... no explanation on what is "truly demonstrable need". Because one can always revert to non inner classes or even to C-style there might be no such need ever.
Dec 05 2006
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Karen Lanrap" <karen digitaldaemon.com> wrote in message 
news:Xns989113480AE6Fdigitaldaemoncom 63.105.9.61...

 That is the usual type of argument ... no explanation on what is
 "truly demonstrable need".

 Because one can always revert to non inner classes or even to C-style
 there might be no such need ever.

Well also we all know how stubborn Walter can be about adding or changing things sometimes. Though as of late he's been pretty malleable..
Dec 05 2006