Raul Laasner
Raul Laasner

Reputation: 1585

protected not visible outside the module

The following code snippet

module test
  private
  protected :: a
  integer :: a = 0
end module test
  use test
  implicit none
  print*, a
end program

results in an error, with both gfortran and ifort, that the variable 'a' must have an explicit type. I also get an error if 'protected' is moved into the line where 'a' is declared. I don't get an error if 'a' has the public attribute (remove the 'private' keyword, put 'public :: a' after 'private', or I put 'public' into the same line as 'a = 0'). I get the desired read-only public behavior only with

  public :: a
  integer, protected :: a = 0

So, unless 'protected' is accompanied by 'public' it acts like 'private'. Am I doing something wrong? I am trying to follow "Modern Fortran" by Clerman and Spector where they suggest the module header to always have the form

private
protected :: <access-id-list>
public :: <access-id-list>

Upvotes: 1

Views: 243

Answers (1)

francescalus
francescalus

Reputation: 32366

The possible accessibility attributes for an object are public and private. protected is not an accessibilty attribute, although the grouping of the three may well suggest that.

In particular, specifying the protected attribute doesn't imply the public attribute so the default accessibility of private applies to the variable a.

Upvotes: 3

Related Questions