inheritance - Fortran Preprocessor Directive Usage -


alright, may using in way wasn't supposed be, here problem. i'm trying make array can hold different types extend single parent type. (via post https://software.intel.com/en-us/forums/topic/280765) suggested follows.

program main  implicit none  type shape     integer :: x,y end type shape  type shape_c     class(shape), pointer :: s end type shape_c  type, extends(shape) :: rectangle     integer :: w,h end type rectangle  type, extends(shape) :: circle     integer :: r end type  type(rectangle) :: r type(circle) :: c type(shape) :: s  class(shape_c), dimension(:), allocatable :: shapes  r = initrect() c = initcircle() s = initshape()  allocate(shapes(3))  shapes(1)%s => r shapes(2)%s => c shapes(3)%s => s  write(*,*) shapes(1)%s%x * shapes(2)%s%x * shapes(3)%s%x  end program main 

at end of post suggests use fpp avoid having write '%s' after shapes every time. attempted use below below program main.

#define shapes(m) shapes(m)%s 

this of course works great except in case of 'allocate(shapes(3))' breaks code. @ loss how avoid happening.

so, may question is, there better way array of inherited variables, way avoid breaking during allocate statement, or solution problem. also, decent guide how use fpp helpful. https://software.intel.com/en-us/node/510271 using i'm sure helpful if had ever used preprocessors before, not useful hasn't.

i'm using intel visual fortran 2015 , vs2012.

both shapes(m) , shapes(m)%s have meaning meaning in fortran , redefining shapes(m) shapes(m)%s breaks code. should use non-fortran keyword when defining macro or @ least use word not present elsewhere. example,

#define shapes(m) shapes(m)%s 

(it common use caps preprocessor macros) or shorter if insist on saving keystrokes. being said, using preprocessor shortening code perhaps not best style , might hurt readability. suggest using capabilities of text editor inserting relevant code bits.


Comments