Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Perhaps you want to do a dynamic_cast&lt;></p> <p>You may want C++ to behave like some language with reflection such as C#. That is not true.<br> This is another post that is similar. </p> <pre><code>http://stackoverflow.com/questions/3049404/apples-oranges-and-pointers-to-the-most-derived-c-class </code></pre> <p>You may be asking something of C++ that it does not have.</p> <pre><code>class basex { public: virtual ~basex() {}; }; class middlex : public basex { public: //middlex() = default; }; int dynamic_test() { basex * cbq = new middlex; basex * cbq2 = new basex; middlex * mcp = dynamic_cast&lt;middlex *&gt;(cbq); //should suceed if (nullptr != mcp) { cout &lt;&lt; "dynamic cast worked" &lt;&lt; endl; } else { cout &lt;&lt; "dynamic cast failed" &lt;&lt; endl; } //should fail middlex * mcp2 = dynamic_cast&lt;middlex *&gt;(cbq2); if (nullptr != mcp2) { cout &lt;&lt; "dynamic cast worked" &lt;&lt; endl; } else { cout &lt;&lt; "dynamic cast failed" &lt;&lt; endl; } return 0; } </code></pre> <p>You seem like you really want to do simple casts and the member function does not seem useful </p> <p>.....................</p> <pre><code>template &lt; class T &gt; T* GetTop() </code></pre> <p>needs improvement It is simply doing a cast .... and casts require the destination type static_cast requires explicit type argument thus.. GetTop requires explicit type argument</p> <p>GetTop is a member function ... the T can be any class ... the code provided .. has no given parameters for type deduction ... the pointer your provide is ... C++ this ...thus you may call it only TopClass or derived objects. (Lets exclude complicated casts)</p> <pre><code>TopClass * top = m-&gt;GetTop(); </code></pre> <p>Compiler will try to make sense of right side .............not considering left side<br> The compiler does not consider the left side for type deduction.. there is no assistance to type deduction .. it will simply try to make sense of the right side<br> I say that because it seems you might believe the compiler will use the type TopClass for type deduction ... it will not </p> <p>This will work compile </p> <pre><code>TopClass * top = m-&gt;GetTop&lt;TopClass&gt;(); </code></pre> <p>But its doing nothing more than an explicit static_cast</p> <p>Maybe you should state what you are intending to do</p>
    singulars
    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.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      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