How to get class name in method inside this class

Is there a way to get the class name inside a method in one class? Or in general, if I have an instance of a class, and I need to know which class is an instance?

+6
source share
2 answers

In Qore (according to the tag for the question) you need to use the <object> :: className () pseudo method on your object.

Example:

prompt% qore -nX '(new Mutex()).className()'
"Mutex"


If you are in a class, use this pseudo-method for an automatic variable self:

prompt% qore -ne '
class T {
    string getClassName() {
        return self.className();
    }
}
class U inherits T {}
printf("%s\n", (new U()).getClassName());
'
U


Alternatively, you can also use the get_class_name () function , as shown in the following example:

prompt% qore -nX 'get_class_name(new Mutex())'
"Mutex"


: , -, , - , .

+3

All Articles