小编ray*_*ray的帖子

是否可以在Fortran 2003中的类型中实现"抽象"变量?

我想写一个抽象类型

type, abstract :: Vehicle
    real, dimension(:), allocatable:: Wheels 
    contains
     procedure (Compute_Weight), deferred :: VehicleWeight
end type Vehicle
Run Code Online (Sandbox Code Playgroud)

我希望在数组的抽象类型中有一个占位符,以便可以在扩展类型中覆盖或重新定义它

type, extends(Vehicle) :: Bike
     allocate(Wheels(2))
    contains
     procedure :: VehicleWeight => BikeWeight
end type Bike

    type, extends(Vehicle) :: Car
     allocate(Wheels(4))
    contains
     procedure :: VehicleWeight => CarWeight
end type Car
Run Code Online (Sandbox Code Playgroud)

GCC编译器抱怨(理所当然我猜),我能找到解决这个问题的唯一解决办法就是不在抽象类型中声明可分配函数,并直接用类型中的正确大小声明变量.不过,我希望有一种占位符来强制执行Wheels描述的基本属性(原型).一世

abstract-class fortran prototype

3
推荐指数
1
解决办法
270
查看次数

标签 统计

abstract-class ×1

fortran ×1

prototype ×1