2
Answers

Class having itself as member

Justus

Justus

16y
2.3k
1
Hi,
i need a member of a class to be of the same type as the class itself. When i inherit the class, the member should be of the type of the inherited type.

class BaseClass { BaseClass member; }

and

class MyClass : BaseClasse { MyClass member;}

I can achieve this by making BaseClass generic:

class BaseClass<T> { T member; }

and

class MyClass : BaseClass<MyClass> {}

but is there a more elegant way?

Answers (2)