Quantcast
Channel: Intel® Software - Intel® Visual Fortran Compiler for Windows*
Viewing all articles
Browse latest Browse all 5691

Polymorphic array - Assign elements of different types

$
0
0

Hello Forum!

Let's say I have a supertype like this

    type, public :: supertype
        character(len=:), allocatable :: id
        character(len=:), allocatable :: type

        contains
    end type

and multiple subtypes like these

    type, public, extends(supertype) :: subtype1
        type(membertype) :: firstMember
        contains
            procedure :: setFirstMember
            procedure :: getFirstMember
   end type

and then I want an Array like

class(supertype), dimension(:), allocatable :: array

where I can put objects of different subtypes and get them out again, so they retain their subtype when it comes to a "select type" later.

Is this possible? I understand I have to assign the elements to the array by sourced allocation but when I try to assign a value to the third array entry with

type(subtype1) :: subtype1Object

allocate(array(3), source = subtype1Object)

I do not only assign a type and value to array entry 3 but get an array of size 3 with all the entries being copies of subtype1Object.

I hope someone can help me, unfortunately there isn't a lot of literature on Fortran OO out there.


Viewing all articles
Browse latest Browse all 5691

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>