Question

I'm trying to define a Fortran derived type that has a private allocatable array. However, I would like to be able to access the array via a public pointer for use in other modules. E.g.

type,public :: test
private
 real,allocatable :: a(:,:,:)
contains
 real,pointer,dimension(:,:,:),public :: point => a
end type test

I just get a compiler error when attempting it like the above.

Is this possible without writing a subroutine that does the pointing for me?

Was it helpful?

Solution

No.

The syntax error is perhaps because you have the pointer component in the type bound procedure part of the type definition (after the contains), not in the component part (before the contains).

Beyond syntax, there are some problems with what you want to do:

  • You cannot associate a pointer with a component of a type definition. Pointers can be associated with components of objects (a subobject). Similarly, you cannot associate a pointer with something that doesn't have the target attribute. Types and components of types can't have the target attribute. Variables of that type, or objects pointed at by pointer components of an object may have the target attribute.

  • You cannot associate a pointer with something that isn't allocated. If something isn't allocated then there isn't anything to point at.

  • An initializer for a pointer component cannot refer to something that is allocatable. In addition to the target attribute the thing that it refers to must have the SAVE attribute. As the case with the TARGET attribute, variables have the save attribute, not type or component definitions.

  • Associating a pointer with a component of an object may defeat the point of making the component private in the first place. This leads to the question - what are you trying to do?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top