[solved] any workaround for incomplete type ?

General FreeBASIC programming questions.
Post Reply
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

[solved] any workaround for incomplete type ?

Post by D.J.Peters »

I know forward declared UDT's can only be used as UDT ptr
but after many years of this limitation exist a workaround may be with the new macro features ?

Joshy

Code: Select all

type _vec3 as vec3 ' forward declare

type vec2
  declare constructor(byref x as const single=0.0, byref x as const single=0.0)
  declare function xxx as _vec3 ' <--- error 71: Incomplete type
  as single x=any,y=any
end type
constructor vec2(byref a as const single, byref b as const single)
  x=a:y=b
end constructor

type vec3
  declare constructor(byref x as const single=0.0, byref x as const single=0.0, byref z as const single=0.0)
  as single x=any,y=any,z=any
end type
constructor vec3(byref a as const single, byref b as const single, byref c as const single)
  x=a:y=b:z=c
end constructor

function vec2 . xxx as vec3
  vec3(x,x,x)
end function
Last edited by D.J.Peters on May 12, 2021 13:53, edited 1 time in total.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: any workaround for incomplete type ?

Post by fxm »

D.J.Peters wrote:I know forward declared UDT's can only be used 'as UDT ptr'
  • or 'byref as UDT':

    Code: Select all

    type _vec3 as vec3 ' forward declare
    
    type vec2
      declare constructor(byref x as const single=0.0, byref x as const single=0.0)
      declare function xxx() byref as _vec3
      as single x=any,y=any
    end type
    constructor vec2(byref a as const single, byref b as const single)
      x=a:y=b
    end constructor
    
    type vec3
      declare constructor(byref x as const single=0.0, byref x as const single=0.0, byref z as const single=0.0)
      as single x=any,y=any,z=any
    end type
    constructor vec3(byref a as const single, byref b as const single, byref c as const single)
      x=a:y=b:z=c
    end constructor
    
    function vec2.xxx() byref as vec3
      static v as vec3
      v = vec3(1,2,3)
      return v
    end function
    
    dim v2 as vec2, v3 as vec3
    v3 = v2.xxx() ' <--- function always used like a function returning by value (by copying the returned reference)
    
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: any workaround for incomplete type ?

Post by D.J.Peters »

@fxm static allocation and returning byref I see thank you.

Joshy
Post Reply