Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<ul> <li><p>Your classes don't have a virtual function. See <a href="http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.7" rel="nofollow noreferrer">FAQ 20.7</a></p></li> <li><p>Beacuse <code>Parent *&amp;</code> is a reference to a pointer to a <code>Parent</code> object. You are passing a pointer to a <code>Child</code> -- these are incompatible types. You can bind a temporary to a const-reference i.e. if you change your parameter to:</p> <p><code>void RemoveObj(Parent* const&amp; foo);</code></p></li> </ul> <p>But then you won't be able to do much with this.</p> <blockquote> <p>It was just a test code so I didn't make any virtual destructors. If I understand correctly in the second call of RemoveObj(), I get a temporary Parent* object which can be passed as a const reference to the function. Is this correct?</p> </blockquote> <p>I strongly suggest you run the following program in standard C++98 mode, once as in and then again after you have commented out <code>foo(b)</code> and uncommented <code>delete b</code>. Next, try putting in a <code>virtual</code> before <code>~s()</code>. The differences should be self-explanatory!</p> <pre><code>#include &lt;iostream&gt; using namespace std; struct s { s() {cout &lt;&lt; __func__ &lt;&lt; endl; } ~s() {cout &lt;&lt; __func__ &lt;&lt; endl; } }; struct t : s { t() {cout &lt;&lt; __func__ &lt;&lt; endl; } ~t() {cout &lt;&lt; __func__ &lt;&lt; endl; } }; void foo(s* const&amp; x) { delete x; } int main() { t* b = new t; foo(b); //delete b; } </code></pre>
 

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