PROTECTED (Fortran 2003)
Purpose
The PROTECTED attribute allows greater control over the modification of module entities. A module procedure can only modify a protected module entity or its subobjects if the same module defines both the procedure and the entity.
Syntax
Rules
If you specify that an object declared by an EQUIVALENCE statement has the PROTECTED attribute, all objects specified in that EQUIVALENCE statement must have the PROTECTED attribute.
A nonpointer object with the PROTECTED attribute accessed through use association, is not definable.
You must not specify the PROTECTED attribute for integer pointers.
A pointer
object with the PROTECTED attribute accessed
through use association, must not appear as any of the following:
- As a pointer object in a NULLIFY statement or POINTER assignment statement
- As an allocatable object in an ALLOCATE or DEALLOCATE statement.
- As an actual argument in reference to a procedure, if the associated dummy argument is a pointer with the INTENT(INOUT) or INTENT(OUT) attribute.
ALLOCATABLE 1 | INTENT | SAVE |
ASYNCHRONOUS | OPTIONAL | STATIC 3 |
AUTOMATIC 3 | POINTER | TARGET |
CONTIGUOUS 2 | PRIVATE | VOLATILE |
DIMENSION | PUBLIC | |
Note:
|
Examples
In the following example, the
values of both age and val can
only be modified by subroutines in the module in which they are declared:
module mod1
integer, protected :: val
integer :: age
protected :: age
contains
subroutine set_val(arg)
integer arg
val = arg
end subroutine
subroutine set_age(arg)
integer arg
age = arg
end subroutine
end module
program dt_init01
use mod1
implicit none
integer :: value, his_age
call set_val(88)
call set_age(38)
value = val
his_age = age
print *, value, his_age
end program