Class methods in D?
Steven Schveighoffer
schveiguy at yahoo.com
Thu May 3 11:01:27 PDT 2012
On Thu, 03 May 2012 13:48:24 -0400, Mehrdad <wfunction at hotmail.com> wrote:
> On Thursday, 3 May 2012 at 17:45:20 UTC, Steven Schveighoffer wrote:
>> This works:
>>
>> import std.stdio;
>>
>> class A
>> {
>> string name;
>> this() {this.name = typeid(this).name;}
>> }
>>
>> class B : A {}
>>
>> void main()
>> {
>> A b = new B;
>> A a = new A;
>> writefln("A: %s, B: %s", a.name, b.name);
>> }
>>
>> outputs:
>>
>> A: testclassname.A, B: testclassname.B
>>
>> -Steve
>
> Oh, but that's only the name!
>
> The trouble is that window classes have a lot of attributes -- styles,
> background brushes, etc. -- that are all shared across instances.
>
> There is currently no way (that I know of) to allow a subclass to define
> its own attributes, *without* also giving it a chance to define them
> per-instance instead of per-class.
There's the RTInfo method I told you about (recently added) if you want to
stick the information directly into TypeInfo at compile time.
There's also static ctors. Just add a hashtable based on the class name,
and use typeid(this).name as the initial key. You have to handle all the
inheritance of properties yourself, but that shouldn't be too difficult.
-Steve
More information about the Digitalmars-d
mailing list