Sorry for the confusing subject, I couldn't find a concise formulation of my question.

I have a set of classes derived from the same interface so that I can use polymorphism.

I would like to store the name of the class as a string so that it can be retrieved as a static member of the class or by using polymorphism.

Here is the problem expressed as an exercise where you are invited to replace the ??? with the appropriate text, if it is possible. My blind and naive attempts failed.

interface AThing { ??? string name ??? }

class OneThing : AThing { ??? string name ??? "OneThing" ??? }

class OtherThing : AThing { ??? string name ??? "OtherThing" ??? }


void main()
{
   // Accessing name as static information
   writeln(OneThing.name ??? );

   // Accessing name through polymorphism
   AThing tbl;
   tbl ~= new OneThing;
   tbl ~= new OtherThing;
   tbl ~= new OneThing;
   foreach(a; tbl) writeln(a.name???);
}

The only viable solution I found so far is by using distinct member names. In the interface we define name as a property, and in the class we define the static member with another name. Is it possible to avoid the different names ?

Reply via email to