- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello there once again,
I have been playing with some upcasting (and downcasting) with Fortran's polymorphism these days, but I am not really sure about what is allowed.
I am basically creating a derived type ExtList_T which extends List_T. The pointers are downcast and then upcast again - or return a nullpointer in case it is not possible to upcast. The part of interest is:
TYPE(ExtList_T), POINTER :: instance1 TYPE(ExtList_T), POINTER :: instance2 CLASS(ExtList_T), POINTER :: ptr_ext CLASS(List_T), POINTER :: ptr_lst (...) ptr_ext => Cast_ExtList(instance1%last()) PRINT *,"Worked? ",ASSOCIATED(ptr_ext) ptr_lst => instance1%last() ptr_ext => Cast_ExtList(ptr_lst) PRINT *,"Worked? ",ASSOCIATED(ptr_ext)
I expected it to print "Worked? T" in both occasions, but only the second one gets printed properly with Intel Fortran. gfortran prints the expected result in both cases.
The sample code follows attached.
Thanks in advance for your attention!
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Which version of the compiler are you using? I get two "T" results with 16.0.2 (on Windows x64).
Note that in the select type you are "downcasting" as it is typically described - the parent (base) types are at the top of the conceptual inheritance graph, extension (derived) types are underneath them, moving down means going from parent to extension.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Essentially what is being asked:
Given a reference to an object (class), that may be positioned on some layer of an inheritance "onion", locate the layer of onion that represents the desired class (either direction), else return NULL. I do not know if the Fortran standard has envisioned this. The concept could be doable, due to (my assumption) the class object containing a class type ID. If my assumption is correct, it may be possible to write user code (C) that can locate the position of the ID in the "onion" and then return that as reference. This would be error prone should the nested IDs not have a fence on both ends of the ID list.
Jim Dempsey
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
No need for C - the select type construct lets you move down an inheritance hierarchy from parent type to an extension of the parent type in a type safe manner. As the OP has done, if the selector in the opening statement has the target attribute you can then associate a pointer with the object as the extension type.
Moving up the hierarchy is automatic in most cases, due to the type compatibility rules, or you can use the parent component if the parent type is not abstract.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank you very much for your reply.
I was using ifort 14.0.4 on Linux x86_84. Apparently version 15.0.5 and up works fine.
Also thank you very much for the correction about down/upcasting.
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page