# OOP and iso\_c\_binding

**URL:** <https://fortran-lang.discourse.group/t/oop-and-iso-c-binding/7410>\
**Category:** Help\
**Created:** [February 15, 2024, 12:43am UTC](https://fortran-lang.discourse.group/t/oop-and-iso-c-binding/7410 "2024-02-15T00:43:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicholaswogan](https://avatars.discourse-cdn.com/v4/letter/n/f1d935/32.png) [@nicholaswogan](https://fortran-lang.discourse.group/u/nicholaswogan)\
**Post date:** [February 15, 2024, 12:43am UTC](https://fortran-lang.discourse.group/t/oop-and-iso-c-binding/7410/1 "2024-02-15T00:43:02Z")

</div>

I’m wondering if it safe to cast a base class into a parent class in the way shown in `test1` below using iso\_c\_binding. It seems to work for me always.

Is `test1` fundamentally doing a similar thing to `test2`, under the hood?

```fortran
program main
  use iso_c_binding
  implicit none

  type :: ParentType
    integer :: a = 1
  end type

  type, extends(ParentType) :: ChildType
    integer :: b = 2
  end type

  call test1()
  call test2()

contains

  subroutine test1()

    type(ChildType), target :: c
    type(c_ptr) :: ptr
    type(ParentType), pointer :: p

    ptr = c_loc(c)
    call c_f_pointer(ptr, p)
    print*,p%a

  end subroutine

  subroutine test2()

    type(ChildType), target :: c
    type(c_ptr) :: ptr
    class(ParentType), pointer :: p

    p => c
    print*,p%a
  
  end subroutine

end program

```

---

<div class="post-metadata">

**Author:** ![FedericoPerini](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/federicoperini/32/1750_2.png) [@FedericoPerini](https://fortran-lang.discourse.group/u/FedericoPerini)\
**Post date:** [February 15, 2024, 7:24am UTC](https://fortran-lang.discourse.group/t/oop-and-iso-c-binding/7410/2 "2024-02-15T07:24:43Z")

</div>

Unless your purpose is related to C interoperability, I believe the let’s call it “conforming” way to do it would be via the parent accessor:

```fortran
   p => c%ParentType

```

but interesting to see that both of them work (your C method seems to have interesting implications for C APIs with Fortran classes indeed!)

---

<div class="post-metadata">

**Author:** ![davidpfister](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/davidpfister/32/3949_2.png) [@davidpfister](https://fortran-lang.discourse.group/u/davidpfister)\
**Post date:** [February 15, 2024, 12:13pm UTC](https://fortran-lang.discourse.group/t/oop-and-iso-c-binding/7410/3 "2024-02-15T12:13:04Z")

</div>

Type casting using iso\_c\_binding is likely to work but it’s kind of discouraged unless you exactly know what you are doing.  
See this SO question additional [info](https://stackoverflow.com/questions/30067754/fortran-serialization-using-c-loc-and-c-f-pointer)  
I copy the answer here

> Practically, use of C\_LOC and C\_F\_POINTER in this way is likely to work, but formally it is not standard conforming. The type and type parameters of the Fortran pointer passed to C\_F\_POINTER must either be interoperable with the object nominated by the C address or be the same as the type and type parameters of the object that was originally passed to C\_LOC (see the description of the FPTR argument in F2008 15.2.3.3). Depending on what you are trying to serialize, you may also find that formal restrictions on the C\_LOC argument (which are progressively less restrictive in later standards than F2003) come into play.  
> (The C equivalent requires use of unsigned char for this sort of trick - which is not necessarily the same thing as int8\_t.)  
> There are constraints on the items in an EQUIVALENCE set that make that approach not generally applicable (see constraints C591 through C594 in F2008). Interpreting the internal representation of an object through equivalence is also formally subject to the rules around definition and undefinition of variables - see F2008 16.6.6 item 1 in particular.  
> The conforming way to access the representation of one object as a different type in Fortran is through TRANSFER. Be mindful that serialization of the internal representation of derived types with allocatable or pointer components (is that what you mean by dynamic fields?) may not be useful.  
> Depending on circumstance, it may be simpler and more robust to simply store your real time results in an array of the type of the thing to be stored.

To further test your approach you may want to add allocatable components in the Parent or Child type and see how it behaves.

And @FedericoPerini is right, you can always access the parent type from the child. It appears in your type as a normal component.

---

<div class="post-metadata">

**Author:** ![everythingfunctional](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/everythingfunctional/32/176_2.png) [@everythingfunctional](https://fortran-lang.discourse.group/u/everythingfunctional)\
**Post date:** [February 15, 2024, 3:52pm UTC](https://fortran-lang.discourse.group/t/oop-and-iso-c-binding/7410/4 "2024-02-15T15:52:15Z")

</div>

> [@nicholaswogan](#):
>
> if it safe to cast a base class into a parent class in the way shown in `test1`

It is more likely to work than not, in this case. Although as pointed out not technically standard conforming. You’re skipping all of the checking that the compiler is required to do to make sure that it is in fact valid to point `p` at `c`. `test2` is the safe way to do it.

---

<div class="post-metadata">

**Author:** ![nicholaswogan](https://avatars.discourse-cdn.com/v4/letter/n/f1d935/32.png) [@nicholaswogan](https://fortran-lang.discourse.group/u/nicholaswogan)\
**Post date:** [February 15, 2024, 5:22pm UTC](https://fortran-lang.discourse.group/t/oop-and-iso-c-binding/7410/5 "2024-02-15T17:22:41Z")

</div>

Thanks for the replies! All very helpful. I’ll add a little more context: I’ve been using the `test1` type casting method to create a C interface to my Fortran codes for a while now, and it has always worked, even for complicated derived types involving allocatable attributes. Note, I have only tested the Gfortran compiler.

Whether `test1` is acceptable or not, in general is relevant to these C bindings. If `test1` is NOT accetable, then I have to write a bunch of redundant code. For example, get/set functions need to specified two times.

```fortran
  subroutine parenttype_get_a(ptr, a) bind(c)
    type(c_ptr), intent(in) :: ptr
    integer(c_int), intent(out) :: a
    type(ParentType), pointer :: p
    call c_f_pointer(ptr, p)
    a = p%a
  end subroutine

  subroutine childtype_get_a(ptr, a) bind(c)
    type(c_ptr), intent(in) :: ptr
    integer(c_int), intent(out) :: a
    type(ChildType), pointer :: c
    call c_f_pointer(ptr, c)
    a = c%a
  end subroutine

```

---

<div class="post-metadata">

**Author:** ![nicholaswogan](https://avatars.discourse-cdn.com/v4/letter/n/f1d935/32.png) [@nicholaswogan](https://fortran-lang.discourse.group/u/nicholaswogan)\
**Post date:** [February 15, 2024, 5:53pm UTC](https://fortran-lang.discourse.group/t/oop-and-iso-c-binding/7410/6 "2024-02-15T17:53:44Z")

</div>

BTW, it looks like both `test1` in the original post and `test3` (see below) produced nearly the same assembly with Gfortran. Here is test3

```fortran
  subroutine test3()
    type(ChildType), target :: c
    type(c_ptr) :: ptr
    type(ParentType), pointer :: p

    ptr = c_loc(c)
    p => c%ParentType

    print*,p%a
  end subroutine

```

Here is the x86 diff for both assembly outputs (Gfortran 13.2). I can’t read assembly, so I’m not sure if this one line difference is important

 ![Screenshot 2024-02-15 at 9.48.10 AM](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/9/98069565e6c874550ab59be30daf678e8fb7a85d.png)
