{{fbdoc item="title" value="Types as Objects"}}----
An example of the overloadable operators and member procedures

{{fbdoc item="desc"}}
	When a Type structure is used to define a user object, many procedure kinds can also be added to the data field skeleton to simplify its use by any user:
		**""-""** Sub, Function and Properties members.
		**""-""** Constructors and Destructor.
		**""-""** Overload Operators (inside and outside Type definition).
		
	The following example is just a syntactic template for their declarations and definitions:
		{{fbdoc item="filename" value="examples/manual/proguide/object-class.bas"}}%%(freebasic)
'' Sample Type showing available methods and operators
'' Practically this is a pointless example, as the only
'' data member is an Integer.  It serves only as a
'' guide to syntax.
''
'' There are many other combinations that can be
'' used in passed parameters.  For simplicity
'' This example only uses byref and type T
'' where ever possible.

'' The type 'DataType' is included to show where
'' any data type might be used
type DataType as integer

'' The type 'UDT' is included to show where only 
'' a UDT data type can be used
type UDT
  value as DataType
end type

'' Our main type
type T
  value as DataType
  value_array( 0 ) as DataType

  '' let, cast, combined assignment operators,
  '' constructors, and the destructor, must be
  '' declared inside the type.
  ''
  '' Parameters can be passed Byval or Byref
  '' in most (All? - verify this).
  ''
  '' All procs can be overloaded with different
  '' types as parameters.  In many cases this is not
  '' necessary as the TYPE can be coerced and
  '' converted depending on the CAST methods
  '' it exposes.  The compiler will to its best
  '' to evaluate statements and expressions if
  '' there is enough information to complete
  '' the operation.
  ''
  '' For example,
  '' Even though operator += may not be overloaded
  '' but operator let and operator + are, the
  '' compiler will convert the T += datatype
  '' to T = T + datatype.

  '' Nonstatic members must be declared inside the
  '' type.
  ''
  '' All Nonstatic members are implicitly
  '' passed a hidden **this** parameter having
  '' the same type as the TYPE in which they are
  '' declared.
  ''
  '' Nonstatic member overloaded operators do not
  '' return a type.  All operations are done on the
  '' hidden this parameter.
  ''
  '' Properties: Can be value properties or single
  '' indexed value properties
  '' GET/SET methods must be each delcared if used.

  '' Nonstatic Member Declarations:

  '' Memory Allocation/Deallocation
  
  declare operator new ( byval size as uinteger ) as any ptr
  declare operator new[] ( byval size as uinteger ) as any ptr
  declare operator delete ( byval buf as any ptr )
  declare operator delete[] ( byval buf as any ptr )

  '' Assignment

  declare operator let ( byref rhs as T )
  declare operator let ( byref rhs as DataType )

  '' Cast can be overloaded to return multiple types

  declare operator cast () as string
  declare operator cast () as DataType

  '' Combined assignment

  declare operator += ( byref rhs as T )
  declare operator += ( byref rhs as DataType )
  declare operator -= ( byref rhs as DataType )
  declare operator *= ( byref rhs as DataType )
  declare operator /= ( byref rhs as DataType )
  declare operator \= ( byref rhs as DataType )
  declare operator mod= ( byref rhs as DataType )
  declare operator shl= ( byref rhs as DataType )
  declare operator shr= ( byref rhs as DataType )
  declare operator and= ( byref rhs as DataType )
  declare operator or= ( byref rhs as DataType )
  declare operator xor= ( byref rhs as DataType )
  declare operator imp= ( byref rhs as DataType )
  declare operator eqv= ( byref rhs as DataType )
  declare operator ^= ( byref rhs as DataType )
  declare operator &= ( byref rhs as DataType )
  
  '' Address of

  declare operator @ () as DataType ptr

  '' Constructors can be overloaded

  declare constructor ()
  declare constructor ( byref rhs as T )
  declare constructor ( byref rhs as DataType )

  '' There can be only one destructor

  declare destructor ()

  '' Nonstatic member functions and subs
  '' overloaded procs must have different parameters

  declare function f ( ) as DataType
  declare function f ( byref arg1 as DataType ) as DataType

  declare sub s ( )
  declare sub s ( byref arg1 as T )
  declare sub s ( byref arg1 as DataType )

  '' Properties

  declare property p () as DataType
  declare property p ( byref new_value as DataType )

  declare property pidx ( byval index as DataType ) as DataType
  declare property pidx ( byval index as DataType, byref new_value as DataType )

  '' Iterator
  
  declare operator for ()
  declare operator step ()
  declare operator next ( byref cond as T ) as integer

  declare operator for ( byref stp as T )
  declare operator step ( byref stp as T )
  declare operator next ( byref cond as T, byref stp as T ) as integer

end type

'' These must be global procedures
'' Globals are not prefixed with the the TYPE name

'' At least one parameter must be of Type 'T'
'' For simplicity, type 'T' is always given first for binary ops
'' in this example

declare operator - ( byref rhs as T ) as DataType
declare operator not ( byref rhs as T ) as DataType

declare operator -> ( byref rhs as T ) as UDT
declare operator * ( byref rhs as T ) as DataType

declare operator + ( byref lhs as T, byref rhs as DataType ) as DataType
declare operator - ( byref lhs as T, byref rhs as DataType ) as DataType 
declare operator * ( byref lhs as T, byref rhs as DataType ) as DataType
declare operator / ( byref lhs as T, byref rhs as DataType ) as DataType
declare operator \ ( byref lhs as T, byref rhs as DataType ) as DataType
declare operator mod ( byref lhs as T, byref rhs as DataType ) as DataType
declare operator shl ( byref lhs as T, byref rhs as DataType ) as DataType
declare operator shr ( byref lhs as T, byref rhs as DataType ) as DataType
declare operator and ( byref lhs as T, byref rhs as DataType ) as DataType
declare operator or ( byref lhs as T, byref rhs as DataType ) as DataType
declare operator xor ( byref lhs as T, byref rhs as DataType ) as DataType
declare operator imp ( byref lhs as T, byref rhs as DataType ) as DataType
declare operator eqv ( byref lhs as T, byref rhs as DataType ) as DataType
declare operator ^ ( byref lhs as T, byref rhs as DataType ) as DataType
declare operator = ( byref lhs as T, byref rhs as DataType ) as DataType
declare operator <> ( byref lhs as T, byref rhs as DataType ) as DataType
declare operator < ( byref lhs as T, byref rhs as DataType ) as DataType
declare operator > ( byref lhs as T, byref rhs as DataType ) as DataType
declare operator <= ( byref lhs as T, byref rhs as DataType ) as DataType
declare operator >= ( byref lhs as T, byref rhs as DataType ) as DataType
declare operator & ( byref lhs as T, byref rhs as DataType ) as DataType

declare operator abs ( byref arg as UDT ) as double
declare operator fix ( byref arg as UDT ) as double
declare operator frac ( byref arg as UDT ) as double
declare operator int ( byref arg as UDT ) as double
declare operator sgn ( byref arg as UDT ) as double

'' Global procedures (subs and funcs) can also accept the TYPE
'' as a parameter or return it as a value, as could be done
'' in previous versions of FreeBASIC.
'' No example given. See function or sub in the manual.

'' All TYPE members are defined outside the TYPE

'' Nonstatic members must be prefixed with type name
'' in this case 'T'

'' Name resolution in a NAMESPACE is same as other
'' subs/funcs.  Use USING or prefix the namespace name

operator T.new ( byval size as uinteger ) as any ptr
	operator = allocate( size )
end operator
 
operator T.new[] ( byval size as uinteger ) as any ptr
	operator = allocate( size )
end operator

operator T.delete ( byval buf as any ptr )
	deallocate buf
end operator

operator T.delete[] ( byval buf as any ptr )
	deallocate buf
end operator

operator T.let ( byref rhs as T )
  value = rhs.value  
end operator

operator T.let ( byref rhs as DataType )
  value = rhs  
end operator

operator T.cast ( ) as string
  return str( value )
end operator

operator T.cast ( ) as DataType
  return value
end operator

operator T.+= ( byref rhs as T )
  value += rhs.value
end operator

operator T.+= ( byref rhs as DataType )
  value += rhs
end operator

operator T.-= ( byref rhs as DataType )
  value -= rhs
end operator

operator T.*= ( byref rhs as DataType )
  value *= rhs
end operator

operator T./= ( byref rhs as DataType )
  value /= rhs
end operator

operator T.\= ( byref rhs as DataType )
  value \= rhs
end operator

operator T.mod= ( byref rhs as DataType )
  value mod= rhs
end operator

operator T.shl= ( byref rhs as DataType )
  value shl= rhs
end operator

operator T.shr= ( byref rhs as DataType )
  value shr= rhs
end operator

operator T.and= ( byref rhs as DataType )
  value and= rhs
end operator

operator T.or= ( byref rhs as DataType )
  value or= rhs
end operator

operator T.xor= ( byref rhs as DataType )
  value xor= rhs
end operator

operator T.imp= ( byref rhs as DataType )
  value imp= rhs
end operator

operator T.eqv= ( byref rhs as DataType )
  value eqv= rhs
end operator

operator T.^= ( byref rhs as DataType )
  value ^= rhs
end operator

operator T.&= ( byref rhs as DataType )
  dim tmp as string
  tmp &= str( rhs )
end operator

operator T.@ () as DataType ptr
  return( cast( DataType ptr, @this ))
end operator


'' Constructors:

constructor T ()
  '' default constructor
  value = 0
end constructor

constructor T ( byref rhs as T )
  '' copy constructor
  value = rhs.value
end constructor

constructor T ( byref rhs as DataType )
  '' custom constructor
  value = rhs
end constructor

'' There can be only one destructor

destructor T ()
  '' clean-up, none in this example
end destructor



'' Globals must specify all arguments and return type

operator - ( byref rhs as T ) as DataType
  return (-rhs.value)
end operator

operator not ( byref rhs as T ) as DataType
  return (not rhs.value)
end operator

operator -> ( byref rhs as T ) as UDT
  return type(4)
end operator

operator * ( byref rhs as T ) as DataType
  return 5
end operator

operator + ( byref lhs as T, byref rhs as DataType ) as DataType
  return (lhs.value + rhs)
end operator

operator - ( byref lhs as T, byref rhs as DataType ) as DataType
  return (lhs.value - rhs)
end operator

operator * ( byref lhs as T, byref rhs as DataType ) as DataType
  return (lhs.value * rhs)
end operator

operator / ( byref lhs as T, byref rhs as DataType ) as DataType
  return (lhs.value / rhs)
end operator

operator \ ( byref lhs as T, byref rhs as DataType ) as DataType
  return (lhs.value \ rhs)
end operator

operator mod ( byref lhs as T, byref rhs as DataType ) as DataType
  return (lhs.value mod rhs)
end operator

operator shl ( byref lhs as T, byref rhs as DataType ) as DataType
  return (lhs.value shl rhs)
end operator

operator shr ( byref lhs as T, byref rhs as DataType ) as DataType
  return (lhs.value shr rhs)
end operator

operator and ( byref lhs as T, byref rhs as DataType ) as DataType
  return (lhs.value and rhs)
end operator

operator or ( byref lhs as T, byref rhs as DataType ) as DataType
  return (lhs.value or rhs)
end operator

operator xor ( byref lhs as T, byref rhs as DataType ) as DataType
  return (lhs.value xor rhs)
end operator

operator imp ( byref lhs as T, byref rhs as DataType ) as DataType
  return (lhs.value imp rhs)
end operator

operator eqv ( byref lhs as T, byref rhs as DataType ) as DataType
  return (lhs.value eqv rhs)
end operator

operator ^ ( byref lhs as T, byref rhs as DataType ) as DataType
  return (lhs.value ^ rhs)
end operator

operator = ( byref lhs as T, byref rhs as DataType ) as DataType
  return (lhs.value = rhs)
end operator

operator <> ( byref lhs as T, byref rhs as DataType ) as DataType
  return (lhs.value <> rhs)
end operator

operator < ( byref lhs as T, byref rhs as DataType ) as DataType
  return (lhs.value < rhs)
end operator

operator > ( byref lhs as T, byref rhs as DataType ) as DataType
  return (lhs.value > rhs)
end operator

operator <= ( byref lhs as T, byref rhs as DataType ) as DataType
  return (lhs.value <= rhs)
end operator

operator >= ( byref lhs as T, byref rhs as DataType ) as DataType
  return (lhs.value >= rhs)
end operator

operator & ( byref lhs as T, byref rhs as DataType ) as DataType
  return val(lhs.value & rhs)
end operator

operator abs ( byref arg as UDT ) as double
	return abs(arg.value)
end operator

operator fix ( byref arg as UDT ) as double
	return fix(arg.value)
end operator

operator frac ( byref arg as UDT ) as double
	return frac(arg.value)
end operator

operator int ( byref arg as UDT ) as double
	return int(arg.value)
end operator

operator sgn ( byref arg as UDT ) as double
	return sgn(arg.value)
end operator

'' Nonstatic member methods

function T.f ( ) as DataType
  dim x as DataType
  return x
end function

function T.f ( byref arg1 as DataType ) as DataType
  arg1 = this.value
  return value
end function

sub T.s ( )
  '' refer to the type using
  
  '' with block
  with this
	.value = 1
  end with
  
  '' field access
  this.value = 2
  
  '' directly
  value = 3

end sub

sub T.s ( byref arg1 as T )
  value = arg1.value
end sub

sub T.s ( byref arg1 as DataType )
  value = arg1
end sub

property T.p () as DataType
  '' GET property
  return value
end property

property T.p ( byref new_value as DataType )
  '' SET property
  value = new_value
end property

property T.pidx ( byval index as DataType ) as DataType
  '' GET indexed property
  return value_array( index )
end property

property T.pidx ( byval index as DataType, byref new_value as DataType )
  '' SET indexed property
  value_array( index ) = new_value
end property

operator T.for ()
end operator

operator T.step ()
end operator

operator T.next ( byref cond as T ) as integer
  return 0
end operator

operator T.for ( byref stp as T )
end operator 

operator T.step ( byref stp as T )
end operator

operator T.next ( byref cond as T, byref stp as T ) as integer
  return 0
end operator

'' new, delete, delete[]

'' Allocate object
dim X as T ptr = new T

'' Deallocate object
delete X

'' Allocate object vector
dim Xlist as T ptr = new T[10]

'' Deallocate object vector
delete[] Xlist
		%%
{{fbdoc item="see"}}
	- ##[[KeyPgType|Type]]##

{{fbdoc item="back" value="CatPgProgrammer|Programmer's Guide"}}