Note that there are some explanatory texts on larger screens.

plurals
  1. POCalling virtual functions inside member functions
    primarykey
    data
    text
    <p>I'm reading <a href="http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html" rel="nofollow noreferrer">Thinking in C++</a> by Bruce Eckel. In Chapter 15 (Volume 1) under the heading "<strong>Behaviour of virtual functions inside constructor</strong>", he goes</p> <blockquote> <p>What happens if you’re inside a constructor and you call a virtual function? Inside an ordinary member function you can imagine what will happen – the virtual call is resolved at runtime because the object cannot know whether it belongs to the class the member function is in, or some class derived from it. For consistency, you might think this is what should happen inside constructors.</p> </blockquote> <p>Here Bruce's trying to explain that when you call a virtual function inside an object's constructor, polymorphism isn't exhibited i.e. the current class' function will only be called and it will not be some other derived class version of that function. This is valid and I can understand it, since the constructor for a class will not know beforehand if it's running for it or for someother dervied object's creation. Moreover, if it does so, it'll be calling functions on a partially created object, which is disastrous.</p> <p>While my confusion suddenly arose because of the first sentence where he states about the ordinary member function, where he says the virtual call will be resolved @ run-time. But wait, inside any member function of a class, when you call another function (be it virtual or non-virtual) it's own class version will only be called, right? E.g.</p> <pre><code>class A { virtual void add() { subadd(); } virtual subadd() { std::cout &lt;&lt; "A::subadd()\n"; } }; class B : public A { void add() { subadd(); } void subadd() { std::cout &lt;&lt; "B::subadd()\n"; } }; </code></pre> <p>In the above code, in <code>A::add()</code> when a call to <code>subadd()</code> is made, it'll <em>always</em> call <code>A::subadd()</code> and the same holds true for <code>B</code> as well, right? So what is he meaning by "the virtual call is resolved at runtime because the object cannot know whether it belongs to the class the member function is in, or some class derived from it" ?</p> <p>Is he explaining it with respect to a call <em>via a base class pointer</em>? (I really suspect so) In which case he shouldn't be writing "Inside an ordinary member function"; from my understanding so far, any call of a member function <em>from inside another member function</em> of the same class is not polymorphic, please correct me if am getting it wrong.</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.
 

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