Suppose I have a pointer to a polymorphic object:
TYPE, ABSTRACT:: ab
ENT TYPE
TYPE, EXTENDS(ab):: co
INTEGER:: i
ENT TYPE
Class(ab), POINTER:: foo
How do I initialize the object pointed by foo
to be a co
object, in the executable part of the code? I am thinking of something like
foo => co(1) ! WRONG
but this is wrong.
CodePudding user response:
Pointers can only be initialized with a target with the save
attribute or with null()
. If you mean the initialization during compilation in the declaration part.
In the executable part of code you can certainly make the pointer to point to an object. But you need an actual object with the target
attribute or you need to make an anonymous target using the allocate()
statement.
co(1)
is an expression or a value result of an expression. You cannot point to it, it does not have the pointer attribute. You could allocate an anonymous target with this value
allocate(foo, source=co(1))
Consider using allocatable
instead of a pointer
, they should be strongly preferred. If you could use allocatable, it is as simple as
foo = co(1)
thanks to the automatic (re)allocation in the assignment.