Re: [Vala] virtual / override



2008/12/29 Srecko Howard <srecko internode on net>:
Hi all

A quick question for those who are more in the know of all vala stuff.

The virtual / override language options for methods.  I see that the
code will call the appropriate method regardless if you define the
method as virtual and override it.  The difference seems to be about 10
bytes of code if you don't specify that the method is virtual before
overriding it.  So is there a speed improvement by using explicit form?
I'm assuming no lookup for the appropriate function.  Also if you
override the function without specifying override the code size
increases (about 36 bytes).  Any ideas?

Basically what is the recommended way (personally I don't like the C++
virtual methods stuff, Java based person).  Should it be required or
optional?

Well, Java simply inserts the virtual/override information into
classes automatically based on the method signatures - it is all there
implicitly.  Vala simply allows you to choose whether or not to pay
the (very small) performance cost of a virtual method call, based on
whether you know virtual methods are definitely required.

As for Vala calling the right method anyway, it doesn't in the general
polymorphism case.  The difference between virtual and simple method
is like the following, (sorry if I'm explaining very basic stuff here,
I might have misunderstood what you meant.):

class BaseClass {
 public virtual void some_virtual() { ... }
 public void some_simple() { ... }
}

class SubClass : BaseClass {
 public override void some_virtual() { ... }
 public void some_simple() { ... }
}

BaseClass o1 = new BaseClass()
o1.some_virtual() // calls BaseCall.some_virtual

BaseClass o2 = new SubClass()
o2.some_virtual() // calls SubClass.some_virtual
o2.some_simple() // calls BaseClass.some_simple

That is to say, if you think you have a SubClass at the end of a
BaseClass reference, you will only be calling SubClass methods if they
are virtual in BaseClass, and overridden in SubClass.

-- 
Phil Housley



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]