Note that there are some explanatory texts on larger screens.

plurals
  1. POVirtual inheritance and static inheritance - mixing in C++
    text
    copied!<p>If you have something like this:</p> <pre><code>#include &lt;iostream&gt; template&lt;typename T&gt; class A { public: void func() { T::func(); } }; class B : public A&lt;B&gt; { public: virtual void func() { std::cout &lt;&lt; "into func"; } }; class C : public B { }; int main() { C c; c.func(); return 0; } </code></pre> <p>Is func() dynamically dispatched?<br> How could you implement class A such that if B has a virtual override, that it is dynamically dispatched, but statically dispatched if B doesn't?</p> <p>Edit: My code didn't compile? Sorry guys. I'm kinda ill right now. My new code also doesn't compile, but that's part of the question. Also, this question is for me, not the faq.</p> <pre><code>#include &lt;iostream&gt; template&lt;typename T&gt; class A { public: void func() { T::func(); } }; class B : public A&lt;B&gt; { public: virtual void func() { std::cout &lt;&lt; "in B::func()\n"; } }; class C : public B { public: virtual void func() { std::cout &lt;&lt; "in C::func()\n"; } }; class D : public A&lt;D&gt; { void func() { std::cout &lt;&lt; "in D::func()\n"; } }; class E : public D { void func() { std::cout &lt;&lt; "in E::func()\n"; } }; int main() { C c; c.func(); A&lt;B&gt;&amp; ref = c; ref.func(); // Invokes dynamic lookup, as B declared itself virtual A&lt;D&gt;* ptr = new E; ptr-&gt;func(); // Calls D::func statically as D did not declare itself virtual std::cin.get(); return 0; } visual studio 2010\projects\temp\temp\main.cpp(8): error C2352: 'B::func' : illegal call of non-static member function visual studio 2010\projects\temp\temp\main.cpp(15) : see declaration of 'B::func' visual studio 2010\projects\temp\temp\main.cpp(7) : while compiling class template member function 'void A&lt;T&gt;::func(void)' with [ T=B ] visual studio 2010\projects\temp\temp\main.cpp(13) : see reference to class template instantiation 'A&lt;T&gt;' being compiled with [ T=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