1

I have following:

  type t_octree
     real                          :: box(2,3)
     type(t_octree), pointer       :: parent      => NULL()
     type(t_octree), pointer       :: children(:) => NULL()
     contains
     final                         :: DESTROY_OCTREE
  end type t_octree


  interface t_octree
     module procedure              :: INIT_OCTREE
  end interface t_octree

now my constructor or desctructor:

  subroutine INIT_OCTREE( this, num_points, box )

  implicit none

  class(t_octree)      :: this
  integer                :: num_points
  real                   :: box(2,3)
        (...)
  end subroutine INIT_OCTREE

  subroutine DESTROY_OCTREE( this )

  implicit none
  type(t_octree)         :: this
    (....)
  end subroutine DESTROY_OCTREE

Can somebody explains why both work, i.e. if I declare with type(t_octree) :: this and class(t_octree) :: this. I compiled both cases, and they worked. So what is the differences specifically here?

and when should we use which?

ATK
  • 1,296
  • 10
  • 26
  • This is a very important distinction and certainly has been discussed before. Do you know what polymorphism is? – Vladimir F Героям слава Dec 16 '18 at 15:47
  • If you want to ask something more specific than what the two links offer, just edit your question, it can be re-opened. – Vladimir F Героям слава Dec 16 '18 at 15:56
  • Yes I know this and about general OOP. Regarding the duplicate issue. I already looked at this question, but it does not clearly clearify what the differences are between the two declarations as shown in my example. As both work it would be nice to know why or/and the differences, but also those differences in general. – ATK Dec 16 '18 at 15:56
  • it is quite specific, isn't it? I am showing two ways to declare the object `t_octree` and both examples work, so what are the differences between them? – ATK Dec 16 '18 at 15:58
  • OK, please do also look at the other link.If you do indeed know polymorphism in OOP, then the answer that `class` is polymorphic and `type` is not should suffice. – Vladimir F Героям слава Dec 16 '18 at 15:59
  • It is specific but not in a very convenient way. You have the two declarations in two different subroutines. But really, the difference is that one is polymorphic, the other is not. That's it. But you have to know how polymorphism works. – Vladimir F Героям слава Dec 16 '18 at 16:00
  • The other link, https://stackoverflow.com/questions/53421609/polymorphism-in-fortran tells you which should be used when and why. – Vladimir F Героям слава Dec 16 '18 at 16:01
  • Thanks for those links! I think I see what you means. So if I had multiple destructors/constructors they would need a class declarations in the various subroutines? – ATK Dec 16 '18 at 16:02
  • No, you would not. Please take a few minutes to really read those links and understand the answers. Do not just go through the text. – Vladimir F Героям слава Dec 16 '18 at 16:03
  • 1
    The class is polymorphic, that means that the subroutine can also be called with types derived from your type (children). That's basically it. – Vladimir F Героям слава Dec 16 '18 at 16:05
  • Thanks, I think I got it now. If I decide to go with type declaration instead of class, and later extends my type to a child. That child would not be able to execute the constructor/destructor procedure of the parent, and now needs its own unless I change the declaration to class, – ATK Dec 16 '18 at 16:10
  • Yes, that is the point. But be aware, that a constructor is for a single type anyway and it should normally be a function. But as you have it as a subroutine with a `class` argument, you can call it `call t_octree(a_child, ...)`. – Vladimir F Героям слава Dec 16 '18 at 16:20

0 Answers0