[updated topic] member function ptr

General FreeBASIC programming questions.
Posts: 9825
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: [updated topic] member function ptr

Postby fxm » Dec 02, 2019 6:09

fxm wrote:Important restriction:
As commented out in my example, I repeat that this hacking by down-casting the run-time type of an object only works for derived types without data members (having only procedure members and/or static data members). Otherwise, the override derived procedures could attempt to access data members in its type for which no memory was allocated during the initial construction of the object.

We could add safety to avoid to down-cast the run-time type of an object to a derived Type having data member:

Code: Select all

  '' back to regularly scheduled programming
  constructor func_vars( lo sng, hi sng, off sng, amp sng, wave_name as string )
    #define polymorph_hack(fname) _
      Cptr(any ptr ptr, @this)[0] = Cptr(any ptr ptr, @fname())[0]
    select case lcase( left(wave_name,3) )
    case "saw"
      if sizeof(func_sw) = sizeof(func_vars) then  ' func_sw has no data member
        write lo,hi,off,amp:  polymorph_hack(func_sw)
      end if
    case else
      if sizeof(func_tr) = sizeof(func_vars) then  ' func_tr has no data member
        write lo,hi,off,amp*2:  polymorph_hack(func_tr)
      end if
    End Select
  End constructor

Or put the test inside the macro:

Code: Select all

    #define polymorph_hack(fname) _ ' run-time's type up-casting or valid run-time's type down-casting
      if sizeof(fname) <= sizeof(this) then cptr(any ptr ptr, @this)[0] = cptr(any ptr ptr, @fname())[0]
Last edited by fxm on Dec 02, 2019 18:50, edited 6 times in total.
Posts: 1357
Joined: Jun 04, 2005 9:51

Re: [updated topic] member function ptr

Postby dafhi » Dec 02, 2019 7:48

at present, address reassignment is my only intent. my project 'Palettator' has the new framework.

with your help i was able to come up with a wave description in layer constructor,
where previously i also had to hard-code the equivalent wave sequence (saw + tri + tri for example) in the output

Return to “General”

Who is online

Users browsing this forum: No registered users and 2 guests