Protected global variables in Fortran

泪湿孤枕 提交于 2019-12-01 06:20:44
Johny Bergmann

You could use the PROTECTEDattribute in a module. It has been introduced with the Fortran 2003 standard. The procedures in the module can change PROTECTED objects, but not procedures in modules or programes that USE your module.

Example:

module m_test
    integer, protected :: a
    contains
        subroutine init(val)
            integer val            
            a = val
        end subroutine
end module m_test

program test
    use m_test

    call init(5)
    print *, a
    ! if you uncomment these lines, the compiler should flag an error
    !a = 10
    !print *, a
    call init(10)
    print *, a
end program  
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!