Why can't structs be derived from?

Steven Schveighoffer schveiguy at yahoo.com
Wed Mar 16 04:56:29 PDT 2011


On Tue, 15 Mar 2011 20:32:43 -0400, Andrej Mitrovic  
<andrej.mitrovich at gmail.com> wrote:

> On 3/15/11, dsimcha <dsimcha at yahoo.com> wrote:
>> 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;
>> }
>>
>
> Shouldn't base be a pointer? You'll get errors with that code.

The problem is the name.  The original code had a simple un-parameterized  
slist_node type:

struct slist_node
{
    slist_node * next;
}

So when David wrote his response, he meant *that* struct.  However, the  
way he named his parameterized struct, it actually refers to itself.

This is the one I wrote, which should be what the OP wanted:

struct slist_node_t(T)
{
    slist_node base;
    alias base this;

    T data;
}

-Steve


More information about the Digitalmars-d mailing list