Thread overview
[Bug or feature] nested class inheritance
Apr 15, 2007
David Ferenczi
Apr 15, 2007
Thomas Kuehne
Apr 15, 2007
Bradley Smith
Apr 16, 2007
janderson
Apr 15, 2007
David Ferenczi
Apr 15, 2007
BCS
Apr 16, 2007
David Ferenczi
Apr 16, 2007
BCS
Apr 16, 2007
Bradley Smith
Apr 16, 2007
BCS
April 15, 2007
Compiling the code below gives an error message:

----------------8<---------------------------------
int main(char[][] args)
{
    class A
    {
        protected:
            class AA {}
    }

    class B : A
    {
        protected:
            class BB : AA {}
    }

    return 0;
}
----------------8<---------------------------------


test.d(15): class test.main.B.BB super class AA is nested within A, not B


Is it the intended behaviour?
Does it mean that nested classes wont get inherited, thus cannot be used in
the subclass?

Thanks in advance,
regards,
David


April 15, 2007
David Ferenczi schrieb am 2007-04-15:
>
> Compiling the code below gives an error message:
>
> ----------------8<---------------------------------
> int main(char[][] args)
> {
>     class A
>     {
>         protected:
>             class AA {}
>     }
>
>     class B : A
>     {
>         protected:
>             class BB : AA {}
>     }
>
>     return 0;
> }
> ----------------8<---------------------------------
>
>
> test.d(15): class test.main.B.BB super class AA is nested within A, not B
>
>
> Is it the intended behaviour?
Yes

> Does it mean that nested classes wont get inherited, thus cannot be used in the subclass?

No. The classes are defined inside a function and thus:
# (http://www.digitalmars.com/d/function.html)
# Unlike module level declarations, declarations within function scope
# are processed in order.
#

Thomas


April 15, 2007
Thomas Kuehne wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> David Ferenczi schrieb am 2007-04-15:
>> Compiling the code below gives an error message:
>>
>> ----------------8<---------------------------------
>> int main(char[][] args)
>> {
>>     class A
>>     {
>>         protected:
>>             class AA {}
>>     }
>>
>>     class B : A
>>     {
>>         protected:
>>             class BB : AA {}
>>     }
>>
>>     return 0;
>> }
>> ----------------8<---------------------------------
>>
>>
>> test.d(15): class test.main.B.BB super class AA is nested within A, not B
>>
>>
>> Is it the intended behaviour?
> Yes
> 
>> Does it mean that nested classes wont get inherited, thus cannot be used in
>> the subclass?
> 
> No. The classes are defined inside a function and thus:
> # (http://www.digitalmars.com/d/function.html)
> # Unlike module level declarations, declarations within function scope
> # are processed in order.
> #
> 
> Thomas

I don't understand this answer. If the classes are moved to the module level, the same error results. Why?

class A
{
    protected:
        class AA {}
}

class B : A
{
    protected:
        class BB : AA {}
}

int main(char[][] args)
{
    return 0;
}

test.d(10): class test.B.BB super class AA is nested within A, not B
April 15, 2007
>> Compiling the code below gives an error message:
>>
>> ----------------8<---------------------------------
>> int main(char[][] args)
>> {
>>     class A
>>     {
>>         protected:
>>             class AA {}
>>     }
>>
>>     class B : A
>>     {
>>         protected:
>>             class BB : AA {}
>>     }
>>
>>     return 0;
>> }
>> ----------------8<---------------------------------
>>
>>
>> test.d(15): class test.main.B.BB super class AA is nested within A, not B
>>
>>
>> Is it the intended behaviour?
> Yes
> 
>> Does it mean that nested classes wont get inherited, thus cannot be used in the subclass?
> 
> No. The classes are defined inside a function and thus:
> # (http://www.digitalmars.com/d/function.html)
> # Unlike module level declarations, declarations within function scope
> # are processed in order.
> #
> 
> Thomas

Thank you very much for the quick answer.
Maybe I miss some important point, but I don't understand why the processing
order explains the behaviour.

In my original code the situation looks like this:

a.d:
----------------8<---------------------------------
class A
{
    protected:
        class AA {}
}
----------------8<---------------------------------

b.d:
----------------8<---------------------------------

static private import a: A;

class B : A
{
    protected:
        class BB : AA {}
}
----------------8<---------------------------------

What I would like to do is to nest AA class in A. Let another class B in another module inherit from A. Let B have a nested class BB, which inherits from AA.

What should I do to achieve this? Or is it totally worng?

If I put class AA outside A, everything works.

a.d:
----------------8<---------------------------------
class A
{

}

class AA
{

}
----------------8<---------------------------------

b.d:
----------------8<---------------------------------

static private import a: A, AA;

class B : A
{
    protected:
        class BB : AA {}
}
----------------8<---------------------------------

Thank you very much for your help,

David


April 15, 2007
Reply to David,

> Thank you very much for the quick answer.
> Maybe I miss some important point, but I don't understand why the
> processing
> order explains the behaviour.
> In my original code the situation looks like this:
> 
[...]

A nested class can only be derived from by another class nested inside the same class. As far as I am concerned, this is a design bug or mis-feature. I to have wanted to do exactly what you were trying to do. I could be wrong but I can't see any reason that it should be hard to implement.


April 16, 2007
Bradley Smith wrote:
> Thomas Kuehne wrote:
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA1
>>
>> David Ferenczi schrieb am 2007-04-15:
>>> Compiling the code below gives an error message:
>>>
>>> ----------------8<---------------------------------
>>> int main(char[][] args)
>>> {
>>>     class A
>>>     {
>>>         protected:
>>>             class AA {}
>>>     }
>>>
>>>     class B : A
>>>     {
>>>         protected:
>>>             class BB : AA {}
>>>     }
>>>
>>>     return 0;
>>> }
>>> ----------------8<---------------------------------
>>>
>>>
>>> test.d(15): class test.main.B.BB super class AA is nested within A, not B
>>>
>>>
>>> Is it the intended behaviour?
>> Yes
>>
>>> Does it mean that nested classes wont get inherited, thus cannot be used in
>>> the subclass?
>>
>> No. The classes are defined inside a function and thus:
>> # (http://www.digitalmars.com/d/function.html)
>> # Unlike module level declarations, declarations within function scope
>> # are processed in order.
>> #
>>
>> Thomas
> 
> I don't understand this answer. If the classes are moved to the module level, the same error results. Why?
> 
> class A
> {
>     protected:
>         class AA {}
> }
> 
> class B : A
> {
>     protected:
>         class BB : AA {}
> }
> 
> int main(char[][] args)
> {
>     return 0;
> }
> 
> test.d(10): class test.B.BB super class AA is nested within A, not B

Looks like they both need to be in the same scope.  Although I don't see why this restriction couldn't be freed for these cases to work more like they do in C++. ie we still get an error message with:

 class A
 {
     public:
         class AA {}
 }

 class B : A
 {
     protected:
         class BB : A.AA {}
 }


test.d(10): class test.B.BB super class AA is nested within A, not B


-Joel
April 16, 2007
BCS wrote:

> Reply to David,
> 
>> Thank you very much for the quick answer.
>> Maybe I miss some important point, but I don't understand why the
>> processing
>> order explains the behaviour.
>> In my original code the situation looks like this:
>> 
> [...]
> 
> A nested class can only be derived from by another class nested inside the same class. As far as I am concerned, this is a design bug or mis-feature. I to have wanted to do exactly what you were trying to do. I could be wrong but I can't see any reason that it should be hard to implement.

Should we file a bug, or is there one already? (I haven't found)
April 16, 2007
David Ferenczi wrote:
> BCS wrote:
> 
> 
>>Reply to David,
>>
>>
>>>Thank you very much for the quick answer.
>>>Maybe I miss some important point, but I don't understand why the
>>>processing
>>>order explains the behaviour.
>>>In my original code the situation looks like this:
>>>
>>
>>[...]
>>
>>A nested class can only be derived from by another class nested inside the
>>same class. As far as I am concerned, this is a design bug or mis-feature.
>>I to have wanted to do exactly what you were trying to do. I could be
>>wrong but I can't see any reason that it should be hard to implement.
> 
> 
> Should we file a bug, or is there one already? (I haven't found)

I don't think that there is a bug filed. If you want to, go ahead and file one. I'd mark it as a feature request because I think that the current behavior is "correct" according to the spec.
April 16, 2007
BCS wrote:
> David Ferenczi wrote:
>> BCS wrote:
>>
>>
>>> Reply to David,
>>>
>>> [...]
>>>
>>> A nested class can only be derived from by another class nested inside the
>>> same class. As far as I am concerned, this is a design bug or mis-feature.
>>> I to have wanted to do exactly what you were trying to do. I could be
>>> wrong but I can't see any reason that it should be hard to implement.
>>
>>
>> Should we file a bug, or is there one already? (I haven't found)
> 
> I don't think that there is a bug filed. If you want to, go ahead and file one. I'd mark it as a feature request because I think that the current behavior is "correct" according to the spec.

Where is the spec is this behavior mentioned?

Thanks,
  Bradley
April 16, 2007
Reply to Bradley,

> Where is the spec is this behavior mentioned?
> 
> Thanks,
> Bradley

I can't seem to find it. Maybe I'm just rembering a comment from walter.