Abstract / Virtual / Nothing at all

General FreeBASIC programming questions.
agamemnus
Posts: 1842
Joined: Jun 02, 2005 4:48

Abstract / Virtual / Nothing at all

Postby agamemnus » Aug 18, 2013 20:19

Looked at the examples, but I don't see an actual reason to specify whether a function or sub is abstract, or virtual, or neither. Can't the compiler figure that out?

---------------

Edit:

From my conversation with DKL, in IRC, this is what I understand:

Abtract and virtual allow you to set a priority in TYPE inheritance.
Abstract forces errors when you don't have overrides (it requires overrides), and virtual doesn't.

---------------

For future reference:
<Agamemnus> i'd like to see a way for pointers to own new'd data
<Agamemnus> that's my #1 issue with freebasic right now
<Agamemnus> if they don't own them, they can't delete them
<Agamemnus> (to prevent errors)
<Agamemnus> maybe as a compiler parameter.
Last edited by agamemnus on Aug 18, 2013 20:35, edited 1 time in total.
marcov
Posts: 3019
Joined: Jun 16, 2005 9:45
Location: Eindhoven, NL
Contact:

Re: Abstract / Virtual / Nothing at all

Postby marcov » Aug 18, 2013 21:01

agamemnus wrote:Looked at the examples, but I don't see an actual reason to specify whether a function or sub is abstract, or virtual, or neither. Can't the compiler figure that out?


No.

Abstract generates a warning when a virtual function that is not overriden is called, and a runtime error/exception when it is (acctidentally) called. However it is perfectly valid for a method to be not abstract, IOW the compiler needs extra information from the programmer that it really should be abstract.

Virtual more or less says "generate an entry in a (VMT) table for me, and call all functions via this table". This makes the function overridable in descendents. Again, extra information from the programmer, and you don't want all functions to be virtual. (size and speed tradeoff)
Last edited by marcov on Aug 19, 2013 19:46, edited 1 time in total.
agamemnus
Posts: 1842
Joined: Jun 02, 2005 4:48

Re: Abstract / Virtual / Nothing at all

Postby agamemnus » Aug 18, 2013 21:06

Uhuh, got it. Thanks. :-)
fxm
Posts: 9983
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Abstract / Virtual / Nothing at all

Postby fxm » Aug 20, 2013 8:42

agamemnus wrote:Looked at the examples, but I don't see an actual reason to specify whether a function or sub is abstract, ...

The two examples in documentation (at "Virtual" and "Abstract" keywords pages) show that we can simultaneously well process a collection of different derived-types objects, through base-type pointers only, by using dynamic polymorphism (with virtual/abstract procedures) instead to individually process each derived-type object, by using commonly a reference or a pointer of its own derived-type.

Normally only a base-type procedure (or upper in hierarchy) is accessible through a base-type pointer.
When the procedure is virtual, this tells the program to resolve the most-derived version of the procedure (with the same signature) relating to the real object type (and not the pointer type).

Example:

Code: Select all

Type Parent Extends Object
  Declare Sub f_n ()
  Declare Virtual Sub f_v ()
End Type
Sub Parent.f_n ()
  Print "Parent.f_n()"
End Sub
Sub Parent.f_v ()
  Print "Parent.f_v()"
End Sub

Type Child Extends Parent
  Declare Sub f_n ()
  Declare Sub f_v ()
End Type
Sub Child.f_n ()
  Print "Child.f_n()"
End Sub
Sub Child.f_v ()
  Print "Child.f_v()"
End Sub


Dim As Parent p
Dim As Child c

Dim As Parent Ptr pp_to_c = @c
Dim As Child Ptr cp_to_c = @c

 
p.f_n() ' Parent.f_n()
p.f_v() ' Parent.f_v()
Print
 
c.f_n() ' Child.f_n()
c.f_v() ' Child.f_v()
Print
 
pp_to_c->f_n() ' Parent.f_n()
cp_to_c->f_v() ' Child.f_v()
Print

cp_to_c->f_n() ' Child.f_n()
cp_to_c->f_v() ' Child.f_v()
Sleep

Return to “General”

Who is online

Users browsing this forum: Edwardtrole and 6 guests