Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.

Array of pointers

Screeb
Beginner
3,062 Views

Hi, I'm trying to create an array of pointers. Well, the objective is an array of objects, which can contain any object that extends a certain base class.

I have a base class "object", and a derived class "sphere" ("TYPE, extends(object) :: sphere"). Here's an example of what I'm trying to do:

class(object), pointer :: objects(:)
type(sphere), target :: s1

ALLOCATE(objects(0:numObjects-1), mold=s1)
objects(0) => s1


The last line gives error #8524 "The syntax of this data pointer assignment is incorrect: either 'bound spec' or 'bound remapping' is expected in this context. [0]"

However, the following works fine (but I need an array):

class(object), pointer :: obj
type(sphere), target :: s1
obj => s1


One thing that sort of works is "objects(0:0) => s1", but that seems to resize the array to 0:0.

What am I doing wrong?

0 Kudos
1 Solution
Steven_L_Intel1
Employee
3,062 Views
The concept of "array of pointers" does not exist in Fortran. The way you have to do it is have an array of derived type with a pointer component. So something like this:

[fortran]type container
  class(object), pointer :: obj
end type container

type(container), allocatable, dimension(:) :: objects
allocate (objects(0:num_objects))
objects(0)%obj => s1[/fortran]

View solution in original post

0 Kudos
3 Replies
Steven_L_Intel1
Employee
3,063 Views
The concept of "array of pointers" does not exist in Fortran. The way you have to do it is have an array of derived type with a pointer component. So something like this:

[fortran]type container
  class(object), pointer :: obj
end type container

type(container), allocatable, dimension(:) :: objects
allocate (objects(0:num_objects))
objects(0)%obj => s1[/fortran]
0 Kudos
Screeb
Beginner
3,062 Views
Ah, thanks, that works. A bit messier than I would have liked, but oh well.
0 Kudos
jimdempseyatthecove
Honored Contributor III
3,062 Views
Screeb,

You can cleanup the messiness by using an FPP macro.
However, the VS Debugger will not expand the macro (a feature I would like to see added), thus debugging is a bit complicated as you have to manually expand the macro in your watch expressions.

I use this technique a lot since it makes reading the code much clearer.

You can use an inlined function in Fortran on the RHS of =but unfortunately you cannot use it on the LHS of =. You can use an FPPmacro on either side.

Jim Dempsey
0 Kudos
Reply