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.
Abstract / Virtual / Nothing at all
Abstract / Virtual / Nothing at all
Last edited by agamemnus on Aug 18, 2013 20:35, edited 1 time in total.
Re: Abstract / Virtual / Nothing at all
No.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?
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.
Re: Abstract / Virtual / Nothing at all
Uhuh, got it. Thanks. :-)
Re: Abstract / Virtual / Nothing at all
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.agamemnus wrote:Looked at the examples, but I don't see an actual reason to specify whether a function or sub is abstract, ...
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