用户定义的Fortran派生类型实例的构造函数 [英] User defined constructor for Fortran derived type instance

查看:109
本文介绍了用户定义的Fortran派生类型实例的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我与Fortran有关的第二个问题(我使用C ++,所以请原谅我的想法)。

This is my second question related to Fortran (I use C++, so forgive me my way of thinking).

我想使用OOP,也就是说,派生了在适当的时候输入Fortran。
在C ++中,您可以使用用户定义的构造函数,例如 https:/ /msdn.microsoft.com/zh-cn/library/s16xw1a8.aspx

I want to use OOP, to say, derived type in Fortran whenever appropriate. In C++, you can use user defined constructor such as https://msdn.microsoft.com/en-us/library/s16xw1a8.aspx

在Fortran中,情况有所不同。
我尝试的第一件事是从这里:
https://www.ibm.com/developerworks/community/blogs/b10932b4-0edd-4e61-89f2-6e478ccba9aa/entry/object_iented_fortran_user_defined_constructa2> lang = zh-CN

Here in Fortran, things are different. The first thing I tried is from here: https://www.ibm.com/developerworks/community/blogs/b10932b4-0edd-4e61-89f2-6e478ccba9aa/entry/object_oriented_fortran_user_defined_constructors2?lang=en

然后我发现了其他一些方法。
在这里我列出了一些似乎可行的方法,但是我只测试了第一种和第二种方法:

Then I found some other ways to do it. Here I listed a few approaches seemly to work but I only tested the first and second:


  1. 同名通用接口作为它们应该构造的派生类型,请参见上面的链接;

  2. 使用类型绑定过程(这甚至不是传统构造函数)

  1. generic interface with the same name as the derived type they're supposed to construct, see link above;
  2. use type-bound procedure (This is not even a "traditional" constructor)

MODULE mymod
  TYPE mytype
    Private
    INTEGER :: x
    CONTAINS
    PROCEDURE, PASS :: init
  END TYPE
CONTAINS
  SUBROUTINE init(this, i)
    CLASS(mytype), INTENT(OUT) :: this
    INTEGER, INTENT(IN) :: i
    write(*,*) this%x
    IF(i > 0) THEN
      this%x = 1
    ELSE
      this%x = 2
    END IF
    write(*,*) this%x
  END SUBROUTINE init
END
PROGRAM test
  USE mymod
  TYPE(mytype) :: y
  CALL y%init(1)
END PROGRAM


  • 使用静态构造函数或结构构造函数( http://www.lahey.com/docs/lfenthelp/NLMOvUsInvConst.htm
    但似乎这不适用于一般的Fortran http://www.lahey.com/docs/lfenthelp/NLMGSWhatIs .htm

    所以我还不太了解什么是最优选和灵活的初始化方法在实践中/ construct派生类型,特别是当我在开发中使用嵌套派生类型时。希望我能在一些帮助下组织此主题。

    So I haven't understood well enough what is most preferred and flexible approach to initialize/construct a derived type in practice, especially when I use nested derived type in development. I hope I can organize this topic with some help.

    推荐答案

    好的,因此,我假设您在如何在fortran中覆盖结构构造函数,我将回答您的问题在评论中提出。注释中没有足够的地方来回答这个问题。

    OK, so I will assume you read well the answers at How to override a structure constructor in fortran and I will answer your problem raised in the comment. There is not enough place in the comment to answer that.

    您还可以在Fortran中使构造函数接受可变数量的参数。

    You can also make constructors in Fortran which accept variable number of arguments.

    甚至有可能使用默认结构构造函数,每个派生类型默认都具有该构造函数。如果您默认初始化组件,则它在构造函数中是可选的。可分配和指针组件也是如此。

    It is even possible with the default structure constructors which every derived type has by default. If you default-initialize a component, it is optional in the constructor. The same holds for allocatable and pointer components.

    对于类型

    type t1
      integer :: i = 1
      integer, pointer :: ip => null()
      integer, allocatable :: ap
    end type
    

    您可以调用默认构造函数,就像

    you can call the default constructor just as

    instance = t1()
    

    这是完全合法的, i 将为1, ip 将指向 null ,而 ap 将不会分配。

    and it is perfectly legal, i will be 1, ip will point to null and ap will not be allocated.

    或者您可以将其称为

     instance = t1(ap=5)
    

    ap 组件将被分配并设置为5,其他组件将保持默认状态。

    and the ap component will be allocated and set to 5 and the other components will be left default.

    您只需使用参数即可使用用户定义的构造函数获得类似的功能可选

    function t1_user(ap, i) result(res)
      type(t1) :: res
      integer, allocatable :: ap !this argument MUST be passed,
                                 ! it does not have to be allocated
      integer, optional    :: i ! this argument is optional
    
      if (present(i)) then
        ...
      end if
    end function
    

    任何类型绑定过程当然也可以具有可选参数。

    any type-bound procedure can of-course also have optional arguments.

    对于嵌套类型,最好将构造函数用作函数,无论它们是默认值还是用户定义的值:

    As for the nested types, that is really best done with constructors as functions, no matter if they are default or user defined:

    type inner
      real :: x, y
    end type
    
    type outer
      type(inner), allocatable :: in
      real :: z
    end type
    
    instance1 = outer(inner(1., 2.), 3.)
    
    instance2 = outer(z=4.)
    

    这篇关于用户定义的Fortran派生类型实例的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆