<div dir="ltr">On 10 April 2013 20:29, Regan Heath <span dir="ltr"><<a href="mailto:regan@netmail.co.nz" target="_blank">regan@netmail.co.nz</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Ok, lets Rewind for a sec.  I need some educating on the actual issue and I want to go through the problem one case at a time..<br>
<br>
<br>
#1 If I write a class..<br>
<br>
class A<br>
{<br>
  public int isVirt()  { return 1; }<br>
  public int notVirt() { return 2; }<br>
}<br>
<br>
and compile it with another class..<br>
<br>
class B : A<br>
{<br>
  override public int isVirt() { return 5; }<br>
}<br>
<br>
and this main..<br>
<br>
void main()<br>
{<br>
  A a = new B();<br>
  a.isVirt();    // compiler makes a virtual call<br>
  a.notVirt();   // but not here<br>
}<br>
<br>
Right?<br>
<br>
<br>
#2 But, if I do /not/ compile class B and have..<br>
<br>
void main()<br>
{<br>
  A a = new A();<br>
  a.isVirt();     // not a virtual call<br>
  a.notVirt();    // neither is this<br>
}<br>
<br>
Right?<br>
<br>
<br>
#3 If I put class A into a library (lib/dll) then compile/link it with..<br>
<br>
class B : A<br>
{<br>
  override public int isVirt() { return 5; }<br>
}<br>
<br>
and..<br>
<br>
void main()<br>
{<br>
  A a = new B();<br>
  a.isVirt();    // compiler makes a virtual call<br>
  a.notVirt();   // we're saying it has to make a virtual call here too<br>
}<br>
<br>
So, when the compiler produced the library it compiled all methods of A as virtual because it could not know if they were to be overridden in consumers of the library.<br>
<br>
So, if the library created an A and passed it to you, all method calls would have to be virtual.<br>
<br>
And, in your own code, if you derive B from A, then calls to A base class methods will be virtual.<br>
<br>
Right?<span class="HOEnZb"><font color="#888888"><br>
<br>
R<br>
</font></span></blockquote></div><br></div><div class="gmail_extra" style>All your examples all hinge on the one case where the optimisation may possibly be valid, a is _allocated in the same scope_. Consider:</div><div class="gmail_extra" style>
<br></div><div class="gmail_extra" style>void func(A* a)</div><div class="gmail_extra" style>{</div><div class="gmail_extra" style> a.isVirt(); // is it? I guess...</div><div class="gmail_extra" style> a.notVirt(); // what about this?</div>
<div class="gmail_extra" style>}</div><div class="gmail_extra" style><br></div><div class="gmail_extra" style>We don't even know what a B is, or whether it even exists.</div><div class="gmail_extra" style>All we know is that A's methods are virtual by default, and they could be overridden somewhere else. It's not possible to assume otherwise.</div>
</div>