allocatable components. TYPE 構造体の中に、allocatable 属性の変数を入れられるようになりました。Pointer の場合とはメモリー上での連続性や、コピーの時の振る舞いが違うようです。
ソースコード
MODULE real_polunomial_module IMPLICIT NONE TYPE real_polynomial REAL, ALLOCATABLE :: coeff(:) END TYPE real_polynomial INTERFACE OPERATOR(+) MODULE PROCEDURE rp_add_rp END INTERFACE OPERATOR(+) CONTAINS !--------------------------------------- TYPE(real_polynomial) FUNCTION rp_add_rp(p1, p2) TYPE(real_polynomial), INTENT(IN) :: p1, p2 INTEGER :: m, m1, m2 m1 = UBOUND(p1%coeff, 1) m2 = UBOUND(p2%coeff, 1) ALLOCATE( rp_add_rp%coeff( MAX(m1, m2) ) ) m = MIN(m1, m2) rp_add_rp%coeff(:m) = p1%coeff(:m) + p2%coeff(:m) IF (m1 > m) rp_add_rp%coeff(m + 1:) = p1%coeff(m + 1:) IF (m2 > m) rp_add_rp%coeff(m + 1:) = p1%coeff(m + 1:) RETURN END FUNCTION rp_add_rp !--------------------------------------- END MODULE real_polunomial_module !========================================= PROGRAM example USE real_polunomial_module IMPLICIT NONE TYPE (real_polynomial) :: p, q, r p = real_polynomial( [4.0, 2.0, 1.0] ) ! 4 + 2x + x**2 q = real_polynomial( [-1.0, 1.0] ) r = p + q PRINT *, 'Coefficients are: ', r%coeff STOP END PROGRAM example