Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changes between Initial Version and Version 1 of code/howto/Identifier


Ignore:
Timestamp:
Oct 10, 2008, 1:37:07 AM (16 years ago)
Author:
landauf
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • code/howto/Identifier

    v1 v1  
     1= HowTo: Identifiers =
     2[[TracNav(TracNav/TOC_Development)]]
     3[[TOC]]
     4
     5== Description ==
     6'''Important''': Identifiers are only available to classes in the class hierarchy. Read [wiki:howto/ClassHierarchy this] for more information.
     7
     8The 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 [wiki:Identifier this] for more detailed information.
     9
     10This page explains how to work with Identifiers.
     11
     12== Get the Identifier of a class ==
     13To get the Identifier belonging to a class, just use '''Class('''''ClassName''''')''':
     14{{{
     15Identifier* identifier = Class(MyClass);
     16}}}
     17
     18It's also possible to get the Identifier of a class by using a string:
     19{{{
     20Identifier* identifier = ClassByString("MyClass");
     21}}}
     22
     23== Get the Identifier of an object ==
     24If you want to know to which class an object belongs, just use '''getIdentifer()''':
     25
     26{{{
     27BaseObject* object = getSomeObject();
     28
     29Identifer* identifier = object->getIdentifier();
     30}}}
     31
     32Now how do you get information about this class? For example the name? Just call '''getName()''' on the Identifier:
     33{{{
     34std::cout << "The object is a " << identifier->getName() << std::endl;
     35}}}
     36
     37The output of this code might be: "The object is a WorldEntity".
     38
     39== Compare classes ==
     40If 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''''')''':
     41{{{
     42BaseObject* object = getSomeObject();
     43
     44bool isAMyClass = object->getIdentifier()->isA(Class(MyClass));
     45
     46if (isAMyClass)
     47    std::cout << "Object is a MyClass!" << std::endl;
     48}}}
     49
     50You can do this even shorter:
     51{{{
     52object->isA(Class(MyClass));
     53}}}
     54
     55Apart from isA(...), there are other comparision functions:
     56 * isExactlyA
     57 * isChildOf
     58 * isDirectChildOf
     59 * isParentOf
     60 * isDirectParentOf
     61
     62Read [wiki:Identifier#Comparison this] for more information.
     63