get classname in static member

Simen Kjærås simen.kjaras at gmail.com
Wed Mar 28 10:10:19 UTC 2018


On Wednesday, 28 March 2018 at 09:44:35 UTC, number wrote:
> Is there a way to get the classname without specifying the 
> class in the first place as required by classinfo and 
> fullyQualifiedName? extracting it from __FUNCTION__ wouldn't 
> work outside a function, i.e. for a classfield, and 'this' 
> doesn't work in static members.

While this is not available in static functions (since there's no 
instance to point to), typeof(this) is. So to get the class name 
you'd generally use typeof(this).stringof.


> And could somebody explain to me why 'typeid(this).stringof' is 
> returning 'typeid(this)'?

Because that's what you're asking for. :p

typeid returns the run-time (also called dynamic) type of a class 
instance. stringof is a compile-time construct that resolves to a 
compile-time constant string representation of its argument. If 
that argument is a type, it gives the name of the type. If it's 
an expression (such as 1+2, foo(), or typeid(this)), it gives a 
string representation of the expression. For typeid(this), that's 
"typeid(this)".

If you want to know the dynamic type of a class reference at 
run-time, such as in fuu(), you'd need to write 
writeln(typeid(this)).

--
   Simen


More information about the Digitalmars-d-learn mailing list