Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointing to a object's type variable - Fortran

As I understand, a user-derived type's definition can't contain target attributes. E.g., this isn't allowed:

type TestType
    integer, target :: t
end type

However, it's fine for them to be a pointer:

type TestType2
    integer, pointer :: p
end type

My question is, then, how can one use a pointer to point at an object's type variable? For example, if I wanted an object of type(TestType2) to have its p variable point to an object of type(TestType)'s t variable, how would I go about this? For example:

type(TestType) :: tt
type(TestType2) :: tt2
tt%t = 1
tt%p => tt%t

Thanks!

like image 747
Sam Harrison Avatar asked Jun 22 '26 17:06

Sam Harrison


1 Answers

There would be very little sense in

type TestType
    integer, target :: t
end type

because values of type(TestType) may easily come up in contexts where they cannot be a target of a pointer.

As @roygvib comments, you have to give the target attribute to the whole object variable:

type(TestType), target :: tt

then you can make pointers to any of its components.

I could imagine that one could allow giving the target attribute to allocatable structure components in the type declaration, but it is not allowed. Certainly that would not make good sense for regular components.

like image 61
Vladimir F Героям слава Avatar answered Jun 24 '26 20:06

Vladimir F Героям слава



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!