- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
What is the proper FORTRAN way to handle polymorphic containers?
The following code results in "catastrophic error: **Internal compiler error: segmentation violation signal raised** Please report this error along with the circumstances in which it occurred in a Software Problem Report. Note: File and line given may not be explicit cause of this error. " with Intel fortran xe_2011_sp1.7.256 and gives apparently incorrect behviour with sp1.9.293. In this example a simple superclass and subclass are defined with overloaded print and assignment operator. The container, defined using superclass, than should be usable for all subclasses and the container data type is set through allocate (,mold=) during initialisation. The methods called on the items of the container data should then (or I thought so) be bound to the correct type. Unfortunately both print and overloaded assignment in this example use superclass methods irrespective of the actual type of container data. Is there a way to make the addValue method use correct version of assignment for the data type the container has been initialised with???
Any ideas, would be greatly appreciated.
(Replacing assignment with an overloaded copy subroutine does not work either as it is not possible to overload with a different argument type...)
Thanks
Evgeny
Example:
subclass.f90
module subclass_mod
use superclass_mod
type, extends(superclass) :: subclass
integer :: i2
contains
generic, public :: assignment(=) => copySubclass
generic, public :: print=>printSubclass
procedure, private :: copySubclass
procedure, private :: printSubclass
end type
contains
subroutine copySubclass(this, from)
class(subclass), intent (out) :: this
class(subclass), intent (in) :: from
this%i1=from%i1
this%i2=from%i2
end subroutine
!
subroutine printSubclass(this)
class(subclass), intent (in) :: this
print *, "Subclass: ",this%i1,this%i2
end subroutine
end module subclass_mod
superclass.f90
module superclass_mod
type superclass
integer :: i1
contains
generic, public :: assignment(=) => copySuperclass
generic, public :: print=>printSuperclass
procedure, private :: copySuperclass
procedure, private :: printSuperclass
end type
contains
subroutine copySuperclass(this, from)
class(superclass), intent (out) :: this
class(superclass), intent (in) :: from
this%i1=from%i1
end subroutine
subroutine printSuperclass(this)
class(superclass), intent (in) :: this
print *, "Superclass: ",this%i1
end subroutine
end module superclass_mod
container.f90
module polycontainer_mod
use superclass_mod
use subclass_mod
type, public :: container
integer :: number
class(superclass), pointer :: data (:)
contains
procedure, public :: init
procedure, public :: addValue
procedure, public :: printContainer
end type
contains
!
subroutine init(this,value, temp)
class(container), intent (inout) :: this
class(superclass), intent (inout) :: value
integer, intent(in) :: temp
!
this%number =temp
allocate(this%data(this%number),mold=value)
end subroutine
!
subroutine addValue(this,value,position)
class(container) :: this
class(superclass) :: value
integer :: position
this%data(position)=value
end subroutine
subroutine printContainer(this)
class(container), intent (inout) :: this
integer :: i
print *, "Number of elements: ",this%number
do i=1,this%number
call this%data(i)%print()
end do
end subroutine
end module polycontainer_mod
tes.f90
program test
use superclass_mod
use subclass_mod
use polycontainer_mod
type(superclass) :: sup1,sup2
type(subclass) :: sub1,sub2
type(container) :: containerSup, containerSub
sub1%i1=2.; sub1%i2=3.
sub2=sub1
print *, "Container.."
call containerSub%init(sub2,2)
print *, "Before assignment.."
call containerSub%printContainer()
call containerSub%addValue(sub2,1)
print *, "After assignment.."
call containerSub%printContainer()
end program
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

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