我有一个派生类型 ( wrapper
),其中包含另一个派生类型 ( over
)。对于后者,赋值运算符已被重载。由于派生类型的分配是按默认组件进行的,我希望分配两个实例会在某些时候wrapper
调用重载分配。over
但是,使用下面的程序,似乎并非如此。wrapper
仅当我还为包含实例之间的显式赋值而重载赋值时才调用重载赋值over
(通过取消注释代码行)。为什么?我觉得这有点违反直觉。有什么办法可以避免包装类型的重载?
module test_module
implicit none
type :: over
integer :: ii = 0
end type over
type :: wrapper
type(over) :: myover
end type wrapper
interface assignment(=)
module procedure over_assign
!module procedure wrapper_assign
end interface assignment(=)
contains
subroutine over_assign(other, self)
type(over), intent(out) :: other
type(over), intent(in) :: self
print *, "Assignment of over called"
other%ii = -1
end subroutine over_assign
!subroutine wrapper_assign(other, self)
! type(wrapper), intent(out) :: other
! type(wrapper), intent(in) :: self
!
! other%myover = self%myover
!
!end subroutine wrapper_assign
end module test_module
program test
use test_module
implicit none
type(wrapper) :: w1, w2
print *, "Assigning wrapper instances:"
w2 = w1
end program test