As well as allowing module entities to have access control (being public
or private
) modules entities may also have the protect
attribute. A public protected entity may be use associated, but the used entity is subject to restrictions on its use.
module mod
integer, public, protected :: i=1
end module
program test
use mod, only : i
print *, i ! We are allowed to get the value of i
i = 2 ! But we can't change the value
end program test
A public protected target is not allowed to be pointed at outside its module
module mod
integer, public, target, protected :: i
end module mod
program test
use mod, only : i
integer, pointer :: j
j => i ! Not allowed, even though we aren't changing the value of i
end program test
For a public protected pointer in a module the restrictions are different. What is protected is the association status of the pointer
module mod
integer, public, target :: j
integer, public, protected, pointer :: i => j
end module mod
program test
use mod, only : i
i = 2 ! We may change the value of the target, just not the association status
end program test
As with variable pointers, procedure pointers may also be protected, again preventing change of target association.