Re: [sv-ec] virtual methods

From: David Jones <djones_at_.....>
Date: Fri Jan 02 2009 - 05:16:21 PST
> Leaf l = new;
> Derived d = l;
>
> 1. From d, can I access a method M or property P that is defined in Leaf, but not in Derived?

No.

> I understand that if M or P is defined in both Derived and then overriden in Leaf, then d.P (or M) gives me Derived::P (or M), not Leaf::P (or M), but is d.P (or M) legal if there is no Derived::P (or M)?

Methods and properties are different, as there is no such thing as a
virtual property, so lumping P amd M in the same statement will be
confusing. The "virtual" feature works with methods only.

If you define D::P and then Leaf::P, these are two separate
properties. Leaf::P can never be accessed from d.

In your example, d.M()  will call Derived::M if M is not virtual in
Derived, or Leaf::M if it is.

Any access from d must reference a definition available to Derived,
without recourse to Leaf. If you have a member Derived::P then a
reference from d will always pick up Derived::P. It can't see Leaf at
all. You must have a member Derived::M for a call d.M() to be legal;
Leaf::M will be called only if Derived::M exists and is virtual.

>
> 2. Declaring a method as virtual in Derived (e.g., pow()) means that d.pow() will give me Leaf::pow() instead of Derived::pow(). Is there any equivalent of a virtual method for properties? That is, if property P is defined in both Derived and Leaf, is there any way for d.P to give me Derived::P instead of Leaf::P? There does not seem to be.

There is no equivalent.  You can "fake it" by defining a virtual
method getSomeProperty() in Derived, and then implementing it
appropriately in both Derived and all its subclasses. This is common
in OOP.

Would it help you or the committee to understand this better if I
prepared a document explaining how this stuff works under the hood?
Sometimes understanding the implementation helps to understand the
concept. If you want such a document then you MUST reply IMMEDIATELY:
I am 9 hours behind Israel timewise, and I'm about to leave on a
2-week vacation to Hawaii, so if you want this I will have to get to
it today.

-- 
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
Received on Fri Jan 2 05:16:58 2009

This archive was generated by hypermail 2.1.8 : Fri Jan 02 2009 - 05:17:46 PST