forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to set objects (instances of a class) as parameters?

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General
View previous topic :: View next topic  
Author Message
Anonymous
Guest





PostPosted: Sat May 13, 2006 12:38 pm    Post subject: How to set objects (instances of a class) as parameters? Reply with quote

Greetings. I cannot find this in either of my two Fortran texts.

How can I (or can I at all?) set an object as a parameter?
For example, I defined the object class of "color" (each
instance having a color name as a character variable and an
RGB code as a real 3-array), and then I defined a second class
called "spectrum". Each spectrum-type variable contains a list
of color variables.

Can I set a "spectrum" variable as a parameter, by somehow
specifying (in a parameter statement) a list of colors in the
spectrum? The intrinsic constructor does not seem to work
for this.

With many thanks,
Ludwig


Ludwig
Back to top
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 7916
Location: Salford, UK

PostPosted: Sun May 14, 2006 1:30 am    Post subject: How to set objects (instances of a class) as parameters? Reply with quote

Ludwig

It is not clear to me what you would like to be able to do.

Can you post a few lines of code that illustrates what you would like to be able to do.
Back to top
View user's profile Send private message AIM Address
Anonymous
Guest





PostPosted: Sun May 14, 2006 10:46 am    Post subject: How to set objects (instances of a class) as parameters? Reply with quote

Dear Paul,

Hello, and thanks for helping me out.
Here is a sample of what I would like to do, viz...

module rainbow
type color
character(len=6) :: hue
integer, dimension(3) :: rgb
end type color
type spectrum
integer :: num ! number of colors in spectrum
type(color), dimension(4) :: col ! array of colors in spectrum
end type spectrum
type(color), parameter :: red = color ('red ', (/ 255, 0, 0 /))
type(color), parameter :: green = color ('green ', (/ 0,255, 0 /))
type(color), parameter :: blue = color ('blue ', (/ 0, 0,255 /))
type(color), parameter :: yellow = color ('yellow', (/ 255,255, 0 /))
type(color), parameter :: cyan = color ('cyan ', (/ 0,255,255 /))
end module rainbow
!-----------------------
program main
use rainbow
type(spectrum) :: s
s%num = 4
s%col(1) = red
s%col(2) = green
s%col(3) = cyan
s%col(4) = blue
end program main


Setting the "color" constants [red], [green], etc. is straightforward,
as is assigning the "spectrum" variable [s] using those "color" constants.
But I don't see how to define a "spectrum" object as a constant,

What would the intrinsic constructor for "spectrum" look like?
The following...

type(spectrum), parameter :: s = (4,red,green,cyan,blue)

meets with an error message.

With many thanks,
Ludwig


PS: I found Ed Akin's "Object-Oriented Programming Via
Fortran 90/95" to be very helpful as a start on OOP.


Ludwig
Back to top
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 7916
Location: Salford, UK

PostPosted: Sun May 14, 2006 1:56 pm    Post subject: How to set objects (instances of a class) as parameters? Reply with quote

type(spectrum) :: s
s = spectrum(4,(/red,green,cyan,blue/))
Back to top
View user's profile Send private message AIM Address
Anonymous
Guest





PostPosted: Sun May 14, 2006 8:28 pm    Post subject: How to set objects (instances of a class) as parameters? Reply with quote

Dear Paul,

Thank you SO MUCH for your help.

Best regards,
Ludwig

Ludwig
Back to top
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group