HowTo: Identifiers
Table of Contents
Description
Important: Identifiers are only available to classes in the class hierarchy. Read this for more information.
The Identifier is a construct used to identify a class at runtime. It is also a container for several class-related stuff, but this is not topic of this page. Read this for more detailed information.
This page explains how to work with Identifiers.
Get the Identifier of a class
To get the Identifier belonging to a class, just use Class(ClassName):
Identifier* identifier = Class(MyClass);
It's also possible to get the Identifier of a class by using a string:
Identifier* identifier = ClassByString("MyClass");
Get the Identifier of an object
If you want to know to which class an object belongs, just use getIdentifer():
BaseObject* object = getSomeObject(); Identifer* identifier = object->getIdentifier();
Now how do you get information about this class? For example the name? Just call getName() on the Identifier:
std::cout << "The object is a " << identifier->getName() << std::endl;
The output of this code might be: "The object is a WorldEntity".
Compare classes
If you want to know if an object is an instance of a class inheriting from a given class (or it's the class itself), use isA(identifier):
BaseObject* object = getSomeObject(); bool isAMyClass = object->getIdentifier()->isA(Class(MyClass)); if (isAMyClass) std::cout << "Object is a MyClass!" << std::endl;
You can do this even shorter:
object->isA(Class(MyClass));
Or in combination with a classname in a string:
BaseObject* object = getSomeObject(); std::string classname = getSomeClass(); if (object->isA(ClassByString(classname))) std::cout << "Object is a " << classname << "!" << std::endl;
Apart from isA(…), there are other comparision functions:
- isExactlyA
- isChildOf
- isDirectChildOf
- isParentOf
- isDirectParentOf
Read this for more information.