Like getClass()

Tim M a at b.com
Fri Oct 17 15:00:02 PDT 2008


Did you ever figure out what you were trying to solve. typeof is evaluated  
at compile time so not very useful. If it helps you or anyone else reading  
this some sample code you may find usefull:

module test;

import tango.io.Stdout;

Object dupClass(Object o)
{
         Object newObj = o.classinfo.create;
         return newObj;
}

class Animal
{
         this(char[] name)
         {
                 this.name = name;
         }
         this()
         {
                 //
         }
         public char[] name;
         char[] kind()
         {
                 return "animal";
         }
}

class Dog : Animal
{
         this(char[] name)
         {
                 super(name);
         }
         this()
         {
                 super();
         }
         char[] kind()
         {
                 return "Dog";
         }
}

class Cat : Animal
{
         this(char[] name)
         {
                 super(name);
         }
         this()
         {
                 super();
         }
         char[] kind()
         {
                 return "Cat";
         }
}

int main(char[][] args)
{
         Animal[] animals;

         Dog dog = new Dog("Scooby");
         Cat cat = new Cat("Garfield");

         /* create a new object of the same class as dog */
         animals ~= cast(Animal) dupClass(dog);

         /* new dog needs a name */
         animals[0].name = "Lassy";


         /* add the others to the array */
         animals ~= dog;
         animals ~= cat;

         foreach(Animal anim; animals)
         {
                 Stdout(anim.kind~" : "~anim.name).newline;
         }

         return 0;
}



On Fri, 03 Oct 2008 14:44:11 +1300, Sergey Gromov <snake.scaly at gmail.com>  
wrote:

> Mon, 29 Sep 2008 03:02:58 +0400,
> Sergey Gromov wrote:
>> Mon, 29 Sep 2008 01:12:45 +0400,
>> Denis Koroskin wrote:
>> > On Sun, 28 Sep 2008 04:42:15 +0400, Sergey Gromov  
>> <snake.scaly at gmail.com>
>> > wrote:
>> >
>> > > Sat, 27 Sep 2008 19:21:56 -0400,
>> > > Christopher Wright wrote:
>> > >> Since .classinfo is essentially a virtual call, and interface  
>> vtbls are
>> > >> filled from the class's vtbl, it shouldn't in theory be terribly
>> > >> difficult to change this. Of course, I'd have to check the dmd  
>> source to
>> > >> be sure.
>> > >
>> > > I'd say it's a bug.  Classinfo is for runtime class, and runtime  
>> class
>> > > can never be an interface.
>> >
>> > Do all IUknown interface instances have classinfo member? That's not  
>> a bug
>> > if they don't. That's the same reason why interfaces can't be casted  
>> to
>> > Object.
>>
>> IUnknown cannot be a D interface because it does not comply with D ABI.
>
> My apologies.  I didn't know that IUnknown were directly supported by a
> compiler as a special case of a super-interface.  Everything you say
> seems to be relevant now.



More information about the Digitalmars-d-learn mailing list