Why can't structs be derived from?

Steven Schveighoffer schveiguy at yahoo.com
Tue Mar 15 06:32:02 PDT 2011


On Tue, 15 Mar 2011 09:25:13 -0400, Jens <jne at somewhere.org> 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;
> };
>
>

struct slist_node
{
    slist_node* next;
}

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

should do the trick.  But I would point out that you are better off not  
doing a s-list this way, since traversing will require much casting.

-Steve


More information about the Digitalmars-d mailing list