Show/Hide Toolbars

XSharp

The -vo3 option directs the compiler to treat all methods (including ACCESS and ASSIGN methods) as virtual.

Syntax

-vo3[+|-]

Arguments

+ | - Specifying +, or just -vo3, directs the compiler to treat all methods (including ACCESS and ASSIGN methods) as virtual, regardless of whether the VIRTUAL modifier is used or not. This provides compatibility with the Visual Objects inheritance model.

Remarks

A class method may always be explicitly declared as a virtual method by using the VIRTUAL keyword, regardless of whether -vo3 is used or not.

 

For performance reasons, this option is off by default. Virtual methods incur a slight performance penalty since the actual method implementation that is called cannot be determined until run-time, and depends on the run-time type of the instance on which the invocation takes place. In contrast, calls to non-virtual members can be fully resolved at compile time, and the call is always made to the compile-time type of the instance.

 

To set this compiler option in the Visual Studio development environment:

 

1.Open the project's Properties page.

2.Click the Dialect tab.

3.Change the value.

4.Click here to see the property page

Example

CLASS BaseClass
METHOD WhoAmI() AS STRING
RETURN "BaseClass"
END CLASS
 
CLASS DerivedClass INHERIT BaseClass
METHOD WhoAmI() AS STRING
RETURN "DerivedClass"
END CLASS
 
FUNCTION Start() AS VOID
LOCAL c AS BaseClass
c := DerivedClass{}
? c:WhoAmI()
RETURN
 
// Output using default or /vo3-: BaseClass
// Output using /vo3 or /vo3+: DerivedClass

 

 

When The -vo3 switch is not used, the call to 'c:WhoAmI()' always resolves to the implementation in BaseClass, since the variable 'c' is typed as 'BaseClass' and 'BaseClass.WhoAmI' is a non-virtual method.

 

When The -vo3 switch is used, the call to 'c:WhoAmI()' resolves to the implementation in 'DerivedClass'. Even though the variable 'c' is typed as 'BaseClass', the actual type of the instance stored in 'c' at runtime determines what implementation of 'WhoAmI' to invoke since 'BaseClass.WhoAmI' is a virtual method.

 

The same behavior without using -vo3 could be obtained by doing:

 

VIRTUAL METHOD WhoAmI() AS STRING CLASS BaseClass
  ...

 

This is preferable over using -vo3 since you have explicit control over which methods are and are not virtual, and no unnecessary overhead is incurred where virtual inheritance is not required. However, existing Visual Objects code may not work properly without -vo3, and it may not be practical to modify existing code and add the VIRTUAL keyword to those methods that really need it.