While this may seem confusing at first, if you look at the code you will see a
recurring pattern. The pointer elements are selected using the pointer index method,
followed by the dot notation to select the individual type elements. You start with the
base type, in this case myType, index the pointer, and then select the elements using the
dot notation, in this case darray. Since darray is a pointer, it is selected using a pointer
index, followed by the dot notation to select the row and col elements. The pattern,
pointer index, dot notation, pointer index, dot notation would be used for as many levels
as needed to resolve a individual element within the memory array.
The following diagram shows the memory layout of the program.
You can see the pattern reflected in the diagram. If row were a type pointer, then
you would just repeat the pattern of index, dot notation on row to resolve the next level
of elements. This type of data structure gives you a lot of flexibility in your programming.
There is no need to have three darray elements for each base element; myType may
point to 4 elements and myType may point to 1 element. You would need to add an
additional field in myType to indicate how many darray elements were each myType
element, but that is a minor adjustment and easily programmed. What this concept gives
you is tight control over your data structures, efficient use of memory and at a relatively
low cost in code.
Function Pointers in Types
Once you have created a type definition, you will usually need to create one or
more subroutines or functions that act on that data. By using function pointers, you can
group the code that acts on this data right along with the data itself. This gives you a
powerful capability to organize your data into code objects that operate as a single unit.
This is one of the ideas behind OOP, or object oriented programming; encapsulating data
and the methods (subroutines and functions) that operate on that data into a single
entity. While FreeBasic doesn't yet support object oriented programming, you can derive
some of the benefits of OOP by using functions pointers along with data when you create
your type definitions.
You define a function (or subroutine) pointer by declaring a prototype function
declaration with the type element name. The following code snippet shows a typical
The type definition is defined using data in the normal manner, along with a field,
myFunc, that is defined As Function. When a field is defined As Function or As Sub, this
creates a pointer to the function or subroutine. Notice that the type definition of myFunc
doesn't include a function, but does include the parameter and return types. Since this is
a pointer field, the name isn't required, but the parameters and return type in the
prototype declaration, must match the actual function in order for the compiler to do type
checking on the pointer field.
The Declare statement following the type is needed as a forward reference so that
the compiler knows that a function is defined somewhere in the code. If you left out the
declaration, you would get a variable not declared error when trying the to compile the
code. You create an instance of the type using the Dim statement, just as you have seen
in the other examples. Since myFunc is a pointer, you can't use it until you initialize the
pointer and you do this by using the Addressof operator on the real function's name. This
will store the address of the function in myFunc, which is used when calling the function.
The real function must be coded of course, so that you actually have some code to call
when using the function pointer.
The following program illustrates creating and using a function pointer in a type
Listing 10.8: type-func.bas
Lines 4 through 9 define a type with both integer data fields and a
function pointer field. MyFunc is defined As Function along with the function prototype.
This sets up myFunc as a function pointer. The Declaration statement in line 12 is used as
a forward reference to the actual function code. If you did not supply the declaration, you
would get a variable not found error on line 16. Line 14 creates a variable of the type
and line 16 uses the Addressof operator to initialize the function pointer to the address of
typeFunc, the actual function. Lines 18 and 19 initialize the data fields, arg1 and arg2.
Line 21 actually calls the function with the proper arguments. Notice that the dot notation
is used in calling the function, myFunc as well as passing the data to the function. Obj.ret
olds the functions return value.
The program is closed using the Sleep and End commands, followed by the
function definition. Whenever you create a function pointer, you must have some
corresponding function code in order to pass the address of that code to the function
When you run the program, you should see the following output.
Output 10.8: Output of type-func.bas
As you can see, function pointers are quite simple to define and use. Not only is
your data encapsulated within a single code object, the methods that act on that data are
also contained within that same object, giving you a powerful way to organize your data
structures. The example program is quite simple of course, but you can use this concept
to reduce the complexity of your code. Suppose you are writing a game and the enemy
units have been defined using a type definition. By also including the subroutines or
functions that act on that data within the type definition, you have a single code object
that fully describes an enemy. If you need to make changes to the enemy code, you only
have to update a single code object, rather than a scattered bunch of variables and
This method also enables you to pass information to functions or subroutines as a
single unit, by simply declaring a parameter as the type definition, and you have access
to both the data and methods within the called function. This makes the code much more
reliable and easier to maintain. It also enables you to generalize the code so that when
you create these type of objects, they can be used in other programs.
There may be a time when you need to create two type definitions that reference
each other. Since FreeBasic is a single pass compiler, this poses a problem since the
compiler will encounter a reference to a type that hasn't been defined yet. The solution is
to create a forward reference of the second type. You do this by using the Type-As
keywords, without the End Type. For example, suppose you have two types Type1 and
Type2. Type1 references Type2 and Type2 references Type1. It doesn't matter what order
you define the types, you will generate an error in the compiler, because each type has a
reference that hasn't been defined yet. In order for the compiler to compile successfully
you need to create a forward reference to the second type, and then use that reference
in defining the first type. The following code snippet illustrates this concept.
The code TypeFTasType2 creates the forward reference that is in turn used in
the Type1 definition to refer to Type2, fTypeasFT. FT and Type2 are actually the same
thing, FT is just an alias for the Type2 definition. Whenever you need to have one or mote
type definitions refer to each other, you will need to create forward declarations for the
the types that have not been defined when referenced.
There is yet another data type that can be used in type definitions, the bit field. Bit
fields are defined as variable_name: bits As DataType. The variable name must be
followed with a colon and the number of bits, followed by the data type. Only integer
data types are allowed within a bit field. Bit fields are useful when you need to keep track
of boolean type information, such as if a pixel is on or off. The following program
illustrates using bit fields within a type definition.
Listing 10.9: bitfield.bas
Lines 3 through 6 define a type with two bit fields. B1 is defined as 1 bit,
and b2 is defined as 4 bits. Line 8 creates a variable of the type definition. Line 10 sets
b1 to 1. Since b1 is defined a 1 bit, the only valid values are 0 or 1. Line 11 sets b2 to
1101. Here there are four bits so you can have a range of 0000 to 1111. Lines 13 and 14
print out the values of the bits. The program is closed in the usual way.
When you run the program you should see the following output.
Output 10.9: Output of bitfield.bas
The data type of the bit field determines how many bits you can declare in a bit
field. Since an integer is 32 bits long, you could declare up to 32 bits in the field.
However, in most cases you would declare a single bit for each field, and use a number of
fields to define the bit masking that you wish to use. Using a single bit simplifies the
coding you need to do to determine if a bit is set or cleared.
The Field Property
When you create a variable of a type definition, the type is padded in memory. The
padding allows for faster access of the type members since the type fields are aligned on
a 4 byte or Word boundary. However, this can cause problems when trying to read a type
record from a file that is not padded. You can use the use field property to change the
padding of a type definition. The field keyword is used right after the type name and can
have the values 1, for 1 byte alignment (no padding), 2 for 2 byte alignment and 4 for 4
byte alignment. To define a type with no padding you would use the following syntax.
For 2 byte alignment you would use field = 2. If no field = property is assigned,
then the padding will be 4 bytes. If you are reading a type definition created by FreeBasic
using the default alignment, then you do not need to use the field property.
If you reading a Quick Basic type record, then you will need to use field = 1, as QB used
byte alignment by default.
You can initialize a type definition when you dimension the type just as you can
any of the intrinsic variables. The following code snippet illustrates the syntax.
In the Dim statement, the arrow operator is used to signal the compiler that you
are initializing the type variable. The type element values must be enclosed in
parenthesis, and separated by commas. The order of the value list corresponds to the
order of the type elements, where a will be set to 12345, b to 12 and c to “Hello”. The
following short program initializes a type using this syntax.
Documents you may be interested
Documents you may be interested