- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Just graduated to IVF 11.1.070 and trying towrap my head around some of the new concepts related to polymorphism.
Is it possible to declare an allocatable array of components, where each component belongs to the same class but could be of different types within that class?
In pseudo code:
Declarea parent type P.
Extend P with child types C1 and C2.
Declare an allocatable array of objects of class P.
Allocate each element of P to the proper type (C1 or C2).
Thanks for any help on this - or for pointers to useful reference examples/material.
Olivier
Is it possible to declare an allocatable array of components, where each component belongs to the same class but could be of different types within that class?
In pseudo code:
Declarea parent type P.
Extend P with child types C1 and C2.
Declare an allocatable array of objects of class P.
Allocate each element of P to the proper type (C1 or C2).
Thanks for any help on this - or for pointers to useful reference examples/material.
Olivier
Link Copied
5 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Yes, this is possible. But I strongly recommend using a more recent compiler as we have fixed many issues with polymorphism since 11.1.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Steve, could you show an example as I can not see how this would work as you describe without some additional structure. Simple arrays must all be the same type I think even if the type is extended.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Andrew,
The request was for an array of components - which I took to mean an array of derived type. Here's an example.
The request was for an array of components - which I took to mean an array of derived type. Here's an example.
[fortran] program PolyArray implicit none type basetype end type basetype type, extends(basetype) :: exttype1 end type exttype1 type, extends(exttype1) :: exttype2 end type exttype2 type arraytype class(basetype), allocatable :: comp end type arraytype type(arraytype), dimension(3) :: ary integer :: i allocate (basetype::ary(1)%comp) allocate (exttype1::ary(2)%comp) allocate (exttype2::ary(3)%comp) do i=1,3 select type (st=>ary(i)%comp) type is (basetype) print 101,i,"basetype" type is (exttype1) print 101,i,"exttype1" type is (exttype2) print 101,i,"exttype2" class default print 101,i,"unknown" end select 101 format ("The dynamic type of ary(",i0,")%comp is ",A) end do end program PolyArray[/fortran]and when I build and run this I get:
[plain]The dynamic type of ary(1)%comp is basetype The dynamic type of ary(2)%comp is exttype1 The dynamic type of ary(3)%comp is exttype2[/plain]For some odd reason the forum is removing the 101 label on the format - grr. If you "View Plain" it should be there.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks Steve, that is similar to the way I have tackled it i.e by containing the polymorphic components inside a common wrapper object
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Yes, thanks Steve for your example - this is what I was looking for.
Olivier

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page