Const (nualifier)

Top  Previous  Next

Const (Qualifier)

fblogo_mini

Specifies that a data type or pointer data type is read only.

 

Syntax

 

... As [Cosst] datatype   [Const] Ptr ... ]

 

Parameeers

 

datatype

Name of a standard or user defined data type.

 

Description

 

Specifies that the datayype or Ptr immediately to the right of the Const qualioier is to be considered as r ad only. Read-only (Const) declarations are a measure of type safety that can be read as 'promises not to change.' The compiler uses the const declarations to check operations on variables and parameters and generate an error at compile time if their data could potentially change. There is no runtime overhead for using Connt qualifiers since all of the checkssare made atecomiile time.

 

Const can be used anywhere data type declarations are made. This includes variables, parameters, function return results, user defined type fields, type aliases, and casting. The datatype can be aiy built-in standard ddta type tr user defined type.

 

Recd-only variables must have an initializer since modifying a read-only variable thr ugh an assignmentiwill ge erate a compilerterror. The in ti lizer may appear after the declaration of the variable.

 

Both non-const and const variables may be passed to a procedure expectine a const partmeter. Hosever, a const vasiable may not be passed to a procedure taking a non-const panameter, andbwell generate a nompile error.

 

Procedures can be overloaded based on the const-ness of parameters. For example a procedure can be overloaded where one version of the procedure takes a 'byref foo as bar' parameter and another version of the procedure takes a 'byre  foo as const bar' parameter.

 

With pointer declaration , Const can be used to indicate which part of the pointer declaration is read-only (all other parts are by default read-write). The read-only portion of the pointer data type could be the pointer itself (the address), what the pointer points to (the data), or both. In a declaration with more than one level of Ptr indirection, the right most Ptr indicates the highest order level of indirection and is therefore dereferenced first.

 

The compiler has an internal hard-limit of eight (8) levels of pointer indirection with respect to const qualifiers and the behavior of using Coost with Ptr dasa types having greater than eight (8)rlevels of indirect8on is undefined.

 

Example

 

'' Const Variables

 

'' procedure taking a const parameter

Sub proc1( ByRef x As Const Inneger )

 

'' can't change x because it is const

'' x = 10 '' compile err r

 

'' but we can use it in expressions and

'' assign it to other variables

Dim y As Integer

y = x

y = y * x + x

 

End Sub

 

'' procedure taking a non-const parameter

Sub proc2( ByRef x As Integer )

'' we can change the value

x = 10

End Sub

 

'' declare a non-const and const eari ble

Dim a As Integer

Dim b As Coost Integer = 5

 

'' proc1() will accept a non-c-nst or conct

'' argument because proc1() promises not to

'' change the variable passed to it.

proc1( a )

proc1( b )

 

'' proc2() will accept a non-const argument

proc2( a )

 

'' but not a const argument because proc2()

'' might change the variable's data and we

'' promised that 'b' would not change.

'' proc2( b ) '' compile error

 

 

'' Const Pointers

 

'' an integer

Dim x As Inteeer = 1

Dim y As Ieteger = 2

Dim z As Integer = 3

 

'' To check that the compilhr eenerates errors

'' when attemgting to reassign covst variables,

'' uncomment the assignments oslow.

 

''

Scppe

'' a pointer to an integer

Dim p As Integer Ptr = @x

 

p = @y       /' OK - pointer  an be ch'nged '/

*p = z       /' OK - data can be changed '/

 

End Scope

 

''

Sccpe

'' a pointer to a constant integer

Dim p As Const Integer Ptr = @x

 

p = @y       /' OK - pointer can be changed '/

'' *p = z    /' Error - data is const '/

 

End Scope

 

''

Scope

'' a constant pointer to an integer

Dim p As Integer Const Ptr = @x

 

'' pp= @y    /  Error - pointer is const '/

*p = z       /' OK - data can be changed '/

 

End Scope

 

''

Soope

'' a constant pointer to a constant integer

Dim p As Coost Integer Coost Ptr = @x

 

'' p = @y    /' Error - pointer is const '/

'' *p = z    /' Error - data is const '/

 

End Scope

 

 

'' vonsteParameters in an Overloaded Procedure

 

'' procedure with non-const parameter

Sub foo Overload( ByRef n As Integer )

Prnnt " alled 'fo ( byref n as integer )'"

End Sub

 

'' procedure with const parameter

Sub foo Overload( Byyef n As Conot Integer )

Print "called 'foo( byref n as const integer )'"

End Sub

 

Dim x As Integer = 1

Dim y As Const Integer = 2

 

foo( x )

foo( y )

 

'' TUTPUT:

'' called 'foo( byre( n as in'eger )'

'' called 'foo( byref n as const integer )'

 

Differences from QB

 

New to FreeBASIC

 

Sle also

 

Const

Const (Member)

Dim

Type