Note that there are some explanatory texts on larger screens.

plurals
  1. PO'typeid' versus 'typeof' in C++
    primarykey
    data
    text
    <p>I am wondering what the difference is between <code>typeid</code> and <code>typeof</code> in C++. Here's what I know:</p> <ul> <li><p><code>typeid</code> is mentioned in the documentation for <a href="http://www.cplusplus.com/reference/typeinfo/type_info/" rel="noreferrer">type_info</a> which is defined in the C++ header file <a href="http://www.cplusplus.com/reference/typeinfo/?kw=typeinfo" rel="noreferrer">typeinfo</a>.</p></li> <li><p><code>typeof</code> is defined in the GCC extension for C and in the C++ <a href="http://en.wikipedia.org/wiki/Boost_C%2B%2B_Libraries" rel="noreferrer">Boost</a> library.</p></li> </ul> <p>Also, here is test code test that I've created where I've discovered that <code>typeid</code> does not return what I expected. Why?</p> <p><strong>main.cpp</strong></p> <pre><code>#include &lt;iostream&gt; #include &lt;typeinfo&gt; //for 'typeid' to work class Person { public: // ... Person members ... virtual ~Person() {} }; class Employee : public Person { // ... Employee members ... }; int main () { Person person; Employee employee; Person *ptr = &amp;employee; int t = 3; std::cout &lt;&lt; typeid(t).name() &lt;&lt; std::endl; std::cout &lt;&lt; typeid(person).name() &lt;&lt; std::endl; // Person (statically known at compile-time) std::cout &lt;&lt; typeid(employee).name() &lt;&lt; std::endl; // Employee (statically known at compile-time) std::cout &lt;&lt; typeid(ptr).name() &lt;&lt; std::endl; // Person * (statically known at compile-time) std::cout &lt;&lt; typeid(*ptr).name() &lt;&lt; std::endl; // Employee (looked up dynamically at run-time // because it is the dereference of a pointer // to a polymorphic class) } </code></pre> <p><strong>output:</strong></p> <pre class="lang-none prettyprint-override"><code>bash-3.2$ g++ -Wall main.cpp -o main bash-3.2$ ./main i 6Person 8Employee P6Person 8Employee </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload