Run-time type information

Run-time type information

En informatique, Run-Time Type Information est utilisé pour signaler la capacité d'un langage de programmation à déterminer le type d'une variable pendant l'exécution d'un programme.

Bien que disponible dans la plupart des langages de programmation, le terme RTTI est souvent utilisé en référence au C++ qui par défaut, détermine le type à la compilation. Ce typage dynamique explicite en C++ – déclaré par l'utilisation de l'opération dynamic_cast<> – diffère de celui automatique de l'Objective-C pour lequel le typage et l'édition de liens sont réalisés lors de l'exécution.

Exemple

Voici un exemple d'utilisation de la RTTI en C++ :

 class point;
 
 class trait {
   public:
     virtual void dessine() = 0; // fonction virtuelle pure : la classe, abstraite, ne peut être instanciée
     virtual ~trait() {};
 };
 
 class segment : public trait {  // classe dérivée de trait : doit implémenter dessine()
   public:
     point intersection(segment T);
     point intersection(arc A);
 };
 
 class arc : public trait {     // classe dérivée de trait : doit implémenter dessine()
   public:
     point intersection(segment T);
     point intersection(arc A);
 };
 
 point intersection(trait &T1, trait &T2) { 
   segment S2 = dynamic_cast<segment &>(T2);
   arc A2 = dynamic_cast<arc &>(T2);
   if(S2) return T1.intersection(S2);
   if(A2) return T1.intersection(A2);
 }

Comme on peut le voir, le but de la dernière fonction est de trouver le point d'intersection de deux traits dont les types (arc ou segment) ne seront connus qu'à l'exécution du programme (c'est tout l'intérêt de l'héritage : avoir des pointeurs de la classe mère sur des instances de classes filles). Le polymorphisme règle le problème pour la première variable (T1) puisque T1.intersection() appellera arc::intersection() ou segment::intersection() suivant le type de T1. Il reste cependant le problème du type de l'argument T2 puisque un simple appel à T1.intersection(T2); génère une erreur du compilateur ne sachant quelle fonction utiliser à l'édition de lien.

Si le typage échoue (ce qui est forcément le cas dans cet exemple, soit pour S2, soit pour A2), le programme peut générer une exception bad_cast.

Voir aussi


Wikimedia Foundation. 2010.

Contenu soumis à la licence CC-BY-SA. Source : Article Run-time type information de Wikipédia en français (auteurs)

Игры ⚽ Поможем сделать НИР

Regardez d'autres dictionnaires:

  • Run-time type information — In programming, RTTI (Run Time Type Information, or Run Time Type Identification) refers to a C++ system that keeps information about an object s data type in memory at runtime. Run time type information can apply to simple data types, such as… …   Wikipedia

  • Run-time algorithm specialisation — In computer science, run time algorithm specialisation is a methodology for creating efficient algorithms for costly computation tasks of certain kinds. The methodology originates in the field of automated theorem proving and, more specifically,… …   Wikipedia

  • Type system — Type systems Type safety Inferred vs. Manifest Dynamic vs. Static Strong vs. Weak Nominal vs. Structural Dependent typing Duck typing Latent typing Linear typing Uniqueness typing …   Wikipedia

  • Type safety — In computer science, type safety is a property of some programming languages that is defined differently by different communities, but most definitions involve the use of a type system to prevent certain erroneous or undesirable program behavior… …   Wikipedia

  • Information security — Components: or qualities, i.e., Confidentiality, Integrity and Availability (CIA). Information Systems are decomposed in three main portions, hardware, software and communications with the purpose to identify and apply information security… …   Wikipedia

  • Information technology audit process — Information technology audit process:Generally Accepted Auditing Standards (GAAS)In 1947, the American Institute of Certified Public Accountants (AICPA) adopted GAAS to establish standards for audits. The standards cover the following three… …   Wikipedia

  • Time for Annihilation — Time for Annihilation...On the Record and On the Road Studio album / Live album by Papa Roach …   Wikipedia

  • Time travel — This article details time travel itself. For other uses, see Time Traveler. Time travel is the concept of moving between different moments in time in a manner analogous to moving between different points in space, either sending objects (or in… …   Wikipedia

  • information system — Introduction       an integrated set of components for collecting, storing, processing, and communicating information (information science). Business firms, other organizations, and individuals in contemporary society rely on information systems… …   Universalium

  • run */*/*/ — I UK [rʌn] / US verb Word forms run : present tense I/you/we/they run he/she/it runs present participle running past tense ran UK [ræn] / US past participle run 1) [intransitive] to move quickly to a place using your legs and feet You ll have to… …   English dictionary

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”