<!-- HTML header for doxygen 1.8.8--> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <!-- For Mobile Devices --> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> <meta name="generator" content="Doxygen 1.8.15"/> <script type="text/javascript" src="jquery.min.js"></script> <title>rttr: rttr_cast vs. dynamic_cast</title> <!--<link href="tabs.css" rel="stylesheet" type="text/css"/>--> <script type="text/javascript" src="dynsections.js"></script> <link rel = "shortcut icon" type = "image/x-icon" href = "favicon.ico"> <link rel = "stylesheet" href = "fonts/ptsans_regular_macroman/stylesheet.css"> <link rel = "stylesheet" href = "fonts/source_code_pro_regular/stylesheet.css"> <link href="doxygen.css" rel="stylesheet" type="text/css" /> <link href="custom-doxygen.css" rel="stylesheet" type="text/css"/> <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css"> <link rel="stylesheet" href="custom-bootstrap.css"> <script src="bootstrap/js/bootstrap.min.js"></script> <script type="text/javascript" src="doxy-boot.js"></script> </head> <body> <!-- <nav class="navbar navbar-default" role="navigation"> <div class="container"> <div class="navbar-header"> <a class="navbar-brand">rttr 0.9.6</a> </div> </div> </nav> --> <div id="top"><!-- do not remove this div, it is closed by doxygen! --> <div class="content" id="content"> <div class="container"> <div class="row"> <div class="col-sm-12 panel panel-default" style="padding-bottom: 15px;"> <div style="margin-bottom: 15px;"> <!-- end header part --> <!-- Generated by Doxygen 1.8.15 --> <script type="text/javascript" src="menudata.js"></script> <script type="text/javascript" src="menu.js"></script> <script type="text/javascript"> /* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */ $(function() { initMenu('',false,false,'search.php','Search'); }); /* @license-end */</script> <div id="main-nav"></div> </div><!-- top --> <div class="PageDoc"><div class="header"> <div class="headertitle"> <div class="title">rttr_cast vs. dynamic_cast </div> </div> </div><!--header--> <div class="contents"> <div class="textblock"><p>The <a class="el" href="namespacerttr.html#ab3dd8b62e5ddca4c1d57cc572ba6bb81">rttr_cast</a> function behaves similarly to the standard C++ <code>dynamic_cast</code>. However, it has some advantages, it doesn't require RTTI support, it works across dynamic library boundaries and it performs faster. Run the <code>benchmark</code> target to perform this test on your local machine.</p> <h2><a class="el" href="namespacerttr.html#ab3dd8b62e5ddca4c1d57cc572ba6bb81" title="Casts the given object of type Source_Type to an object of type Target_Type.">rttr::rttr_cast<T>()</a> </h2> <p>The function <a class="el" href="namespacerttr.html#ab3dd8b62e5ddca4c1d57cc572ba6bb81">rttr_cast</a> allows the client to cast between class hierarchies up and down, cross casts between unrelated classes and even class hierarchies with virtual inheritance. The target type <code>T</code> can be also in the middle of the hierarchy.</p> <div class="fragment"><div class="line"><span class="keyword">struct </span>A { <a class="code" href="rttr__enable_8h.html#a848bcae21d3a54e07ca6450689820a59">RTTR_ENABLE</a>() };</div><div class="line"><span class="keyword">struct </span>B : A { <a class="code" href="rttr__enable_8h.html#a848bcae21d3a54e07ca6450689820a59">RTTR_ENABLE</a>(A) };</div><div class="line"><span class="keyword">struct </span>C : B { <a class="code" href="rttr__enable_8h.html#a848bcae21d3a54e07ca6450689820a59">RTTR_ENABLE</a>(B) };</div><div class="line">C c;</div><div class="line">A* a = &c;</div><div class="line">B* b = rttr_cast<B*>(a); <span class="comment">// successful</span></div></div><!-- fragment --><p>Cross casts are also possible:</p> <div class="fragment"><div class="line"><span class="keyword">struct </span>A { <a class="code" href="rttr__enable_8h.html#a848bcae21d3a54e07ca6450689820a59">RTTR_ENABLE</a>() };</div><div class="line"><span class="keyword">struct </span>B { <a class="code" href="rttr__enable_8h.html#a848bcae21d3a54e07ca6450689820a59">RTTR_ENABLE</a>() };</div><div class="line"><span class="keyword">struct </span>C : A , B { <a class="code" href="rttr__enable_8h.html#a848bcae21d3a54e07ca6450689820a59">RTTR_ENABLE</a>(A, B) };</div><div class="line">C c;</div><div class="line">A* a = &c;</div><div class="line">B* b = rttr_cast<B*>(a); <span class="comment">// successful; remark that class A and B are unrelated from each other</span></div></div><!-- fragment --><p>A <a class="el" href="classrttr_1_1type.html">type</a> object knows from which parent class it is derived. Assumed this information is given via <a class="el" href="rttr__enable_8h.html#a848bcae21d3a54e07ca6450689820a59">RTTR_ENABLE</a>.</p> <dl class="section remark"><dt>Remarks</dt><dd>Because exception are not supported the target type <code>T</code> can only be a pointer type.</dd></dl> <h2>Summary </h2> <ul> <li>when it's necessary to perform down casts or cross casts with a given instance, then use <code>rttr_cast</code></li> </ul> <hr/> <div class="btn btn-default doxy-button"><a class="el" href="rttr_type_class_hierachy_page.html">previous</a></div><div class="btn btn-default doxy-button"><a class="el" href="tutorial_page.html">next</a></div> </div></div><!-- PageDoc --> </div><!-- contents --> <!-- HTML footer for doxygen 1.8.9.1--> <!-- start footer part --> <hr class="footer"/> <address class="footer"> <small> Generated on Thu Apr 11 2019 20:05:57 for rttr - 0.9.6 by <a href="http://www.doxygen.org/index.html">doxygen</a>. </small> </address> </body> </html>