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

The PROTECTED attribute must only appear in the specification part of the module.
Read syntax diagramSkip visual syntax diagram
>>-PROTECTED--+----+--entity_declaration_list------------------><
              '-::-'                            

entity
A named variable not in a common block.

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:
Table 1. Attributes compatible with the PROTECTED attribute
ALLOCATABLE  1  INTENT SAVE
ASYNCHRONOUS OPTIONAL STATIC  3 
AUTOMATIC  3  POINTER TARGET
CONTIGUOUS  2  PRIVATE VOLATILE
DIMENSION PUBLIC  
Note:
  •  1  Fortran 2003
  •  2  Fortran 2008
  •  3  IBM extension

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

Related information



Voice your opinion on getting help information Ask IBM compiler experts a technical question in the IBM XL compilers forum Reach out to us