www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - get classname in static member

reply number <putimalitze gmx.de> writes:
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.

It's just about simple debugging like 'writeln(<classname>, ": 
message: ..")' ..and learning.

And could somebody explain to me why 'typeid(this).stringof' is 
returning 'typeid(this)'?
I'm using dmd v2.078.3

```
import std.stdio;

void main() {

	class C
	{
		static void foo()
		{
			writeln(__FUNCTION__);          // test.main.C.foo
			writeln(C.classinfo);           // test.main.C
			import std.traits;              //
			writeln(fullyQualifiedName!C);  // test.main.C
		}

		void fuu()
		{
			writeln(this);                  // test.main.C
			writeln(typeid(this));          // test.main.C
			writeln(typeid(this).stringof); // typeid(this)
			writeln(this.classinfo);        // test.main.C
		}
	}

	C.foo();
	writeln();
	C c = new C();
	c.fuu;
}		
```
Mar 28 2018
next sibling parent reply Simen =?UTF-8?B?S2rDpnLDpXM=?= <simen.kjaras gmail.com> writes:
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
Mar 28 2018
parent number <putimalitze gmx.de> writes:
On Wednesday, 28 March 2018 at 10:10:19 UTC, Simen Kjærås wrote:
 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
On Wednesday, 28 March 2018 at 10:12:34 UTC, bauss wrote:
 On Wednesday, 28 March 2018 at 09:44:35 UTC, number wrote:
 And could somebody explain to me why 'typeid(this).stringof' 
 is returning 'typeid(this)'?
stringof will return a string equivalent to the expression or symbol's representation, not its definition. Meaning: (212 + 221).stringof == "212 + 221" (new Foo).stringof == "new Foo" etc.
Thank you for the explanations. I somehow forgot about typeof though i must have seen it a lot in 'Ali Cehreli's Book.
Mar 28 2018
prev sibling parent bauss <jj_1337 live.dk> writes:
On Wednesday, 28 March 2018 at 09:44:35 UTC, number wrote:
 And could somebody explain to me why 'typeid(this).stringof' is 
 returning 'typeid(this)'?
stringof will return a string equivalent to the expression or symbol's representation, not its definition. Meaning: (212 + 221).stringof == "212 + 221" (new Foo).stringof == "new Foo" etc.
Mar 28 2018