Abstract base class without actual "Abstract"

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
Rule
Posts: 9
Joined: Nov 03, 2020 19:04
Contact:

Abstract base class without actual "Abstract"

Postby Rule » Nov 23, 2021 22:33

Suppose you'd want to define an abstract base type in the sense that it can not be instantiated while its derivatives can, but it doesn't contain any actual abstract methods, you can accomplish it by declaring its Constructor protected:

Code: Select all

Type BaseType
    '' ...
Protected:
    Declare Constructor
End Type

Type DerivedType Extends BaseType
    '' ...
End Type

Dim b As BaseType    '' Error 204: The default constructor has no public access in 'Dim b As BaseType'
Dim d As DerivedType '' OK
fxm
Moderator
Posts: 10765
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Abstract base class without actual "Abstract"

Postby fxm » Nov 23, 2021 22:45

Not quite.
Try:

Code: Select all

Dim d As DerivedType  '' OK
Dim b As BaseType = d '' OK

We should also declare an explicit copy-constructor but protected:

Code: Select all

Type BaseType
    '' ...
Protected:
    Declare Constructor
    Declare Constructor(Byref As Const BaseType)
End Type

Type DerivedType Extends BaseType
    '' ...
End Type

The default-constructor and the copy-constructor are the only two constructors implicitly defined by the compiler.
Rule
Posts: 9
Joined: Nov 03, 2020 19:04
Contact:

Re: Abstract base class without actual "Abstract"

Postby Rule » Nov 23, 2021 22:52

Heh. Yes, of course, we should declare all constructors protected. Or we could declare the destructor protected.
fxm
Moderator
Posts: 10765
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Abstract base class without actual "Abstract"

Postby fxm » Nov 23, 2021 23:06

Rule wrote:Or we could declare the destructor protected.

Not exactly if you are using New/Delete.
This only prohibits the use of Delete:

Code: Select all

Type BaseType
    Protected:
    Dim As Integer I
    Declare Destructor
End Type

Type DerivedType Extends BaseType
    '' ...
End Type

Dim As BaseType Ptr p = New BaseType  '' OK
Delete p                              '' but NOK
Rule
Posts: 9
Joined: Nov 03, 2020 19:04
Contact:

Re: Abstract base class without actual "Abstract"

Postby Rule » Nov 23, 2021 23:10

fxm wrote:This only prohibits the use of Delete:
Hm. I didn't expect that. Is this by design?
fxm
Moderator
Posts: 10765
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Abstract base class without actual "Abstract"

Postby fxm » Nov 23, 2021 23:20

The difference is:
- When using 'Dim As UDT u' (static instance), the compiler adds code to destroy the instance (by calling the destructor) when the instance goes out of scope.
- When using 'Dim As UDT Ptr p = New UDT' (dynamic instance), the compiler does not add anything and it is up to the user to delete the instance himself when it is no longer needed.

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 3 guests