Why can't structs be derived from?

Jens jne at somewhere.org
Tue Mar 15 10:55:05 PDT 2011


Steven Schveighoffer wrote:
> On Tue, 15 Mar 2011 13:30:00 -0400, Jens <jne at somewhere.org> wrote:
>
>> dsimcha wrote:
>>> On 3/15/2011 9:25 AM, Jens wrote:
>>>> It seems rather fundamental to be able to compose a new struct
>>>> from a given struct using inheritance. Why is this not allowed?
>>>>
>>>> struct slist_node
>>>> {
>>>>      slist_node* next;
>>>> };
>>>>
>>>> template<class T>
>>>> struct slist_node<T>: public slist_node
>>>> {
>>>>      T data;
>>>> };
>>>>
>>>>
>>>
>>> Something that has basically that effect is allowed, just not with
>>> that syntax:
>>>
>>> struct slist_node(T)
>>> {
>>>     slist_node base;
>>>     alias base this;
>>>
>>>     T data;
>>> }
>>
>> That's ugly.
>
> That's all there is.  Structs do not have inheritance, only alias
> this.

Why don't they though? Inheritance does not have to mean polymorphic. It 
can mean composition, like in C++. I don't understand the reason for such 
ugly syntax. 




More information about the Digitalmars-d mailing list