Is the memory address of classinfo the same for all instances of a class?

Steven Schveighoffer schveiguy at yahoo.com
Fri Jul 2 06:34:34 PDT 2010


On Fri, 02 Jul 2010 09:32:39 -0400, Steven Schveighoffer  
<schveiguy at yahoo.com> wrote:

> On Fri, 02 Jul 2010 09:24:20 -0400, Heywood Floyd <soul8o8 at gmail.com>  
> wrote:
>
>>
>> Good day!
>>
>>
>> Consider
>>
>> // - - - -
>> class Foo{}
>> auto one = new Foo();
>> auto two = new Foo();
>> writefln("one: %x  two: %x", &one.classinfo, &two.classinfo);
>> // - - - -
>>
>> For me this results in two identical memory addresses "every time".
>>
>> Can I rely on this?
>> Can I design software based on the assumption that these addresses are  
>> always the same?
>>
>> (I'd like to be able to use the memory address as the key in an  
>> associative array, for quick by-class
>> lookups.)
>
> Use classinfo.name.  The classinfo is the same memory address in the  
> same executable/dynamic library.  If you open another D dynamic library,  
> the classinfo address for the same class may be different, but the name  
> will be the same.
>
> Note that comparing classinfo.names will be just as fast as comparing  
> classinfo addresses if the names are at the same address (which will be  
> true if the classinfo is at the same address) because the string  
> comparison function short-circuits if the addresses are the same.

Duh, just realized, classinfos should use this same method to compare.   
Just use the whole class info as the key, don't take the address.

-Steve


More information about the Digitalmars-d-learn mailing list