Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to link 2 different objects

Status
Not open for further replies.

peterwriesnik

Technical User
Feb 24, 2012
3
AT
hi everyone!

i am looking for a simple way to do the following in Fortran 2003 (using ifort 12.0.0):

module mymod

type, abstract :: type_a
logical val
type(type_b), pointer :: myptr
end type

type, abstract :: type_b
logical val
type(type_b), pointer :: myptr
end type

end module

program main
use mymod
type(type_a), target :: obj_1
type(type_b), target :: obj_2

obj_1%myptr => obj_2
obj_1%myptr%val=.TRUE.

end program

i know the code isn't compiling because val isn't a member of myptr. But is there a simple way to link these two objects using pointers? i basically want an access similar to the one i am using (better: wanting to use) in main.

thanks in advance!
 
sorry about the abstract keywords in the type definitions. they have to be removed.
 
Where is the problem ? Without the abstract keywords, your example compiles perfectly.

François Jacq
 
What is it that you want to achieve ? To build a linked list you do not need an additional variable declared, you could declare a pointer within the derived type variable. This is legal :

Code:
type a
    real rVal
    integer iVal
    type (a), pointer :: pNext
end type

type (a) pHead, pTail, pTemp

...
...

if (.not. associated (pHead)) then
    allocate (pHead, stat = ...)
    nullify (pHead%pNext)
    pTail => pHead
else
    allocate (pTail%pNext, stat = ...)
    pTail => pTail%pNext
    nullify (pTail%pNext)
endif
pTail%rVal = ....
pTail%iVal = ....

This is perfectly standard FORTRAN and works in my prog dozens of times.

Norbert


The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
This is perfectly standard FORTRAN and works in my prog dozens of times.
<blush>
sorry, there i was too fas in typing:

insted of

Code:
type (a) pHead, pTail, pTemp

this line should have been

Code:
type (a), pointer :: pHead, pTail, pTemp
</blush>



The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
Thanks for the prompt replies! I gave it another try and realized that my question is needless. Using the code like

module mymod

type :: type_a
logical val
type(type_b), pointer :: myptr
end type

type :: type_b
logical val
type(type_b), pointer :: myptr
end type

end module

program main
use mymod
type(type_a), target :: obj_1
type(type_b), target :: obj_2

obj_1%myptr => obj_2
obj_1%myptr%val=.TRUE.

write (*,*) obj_1%myptr%val

end program

works perfect! The only thing that is important is the '::' when defining the new types (which is standard since F90). I accidentally left out the '::' operator which seemed to mess up the scope of the definitions. Thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top