PDA

View Full Version : struct initialization problem


stuh505
09-29-2005, 05:05 PM
Edit: It works if I add () to the end


struct foo
(
a, b, c,
fn baz n = sqrt (n + 1),
fn bar x y = print (x ^ 2 + y ^ 2)
)




If I do this:



f1 = foo
f1.a = 1



I get a runtime error. I guess, if values for the properties are not defined during initialization, they are no longer members of that initialization..
There's no problem doing it like this...



f1 = foo 1 2 3
f1.a = 2



..except that if I have 20 members, my initialization is going to be really long, and I wouldn't
be able to leave any of them null to be determined later on. Since they must be initialized at creation, I'd like to set default values for them in the struct definition, so that I don't have to specifically declare them all:




struct foo
(
a=0, b=0, c=0,
fn baz n = sqrt (n + 1),
fn bar x y = print (x ^ 2 + y ^ 2)
)




However, this doesn't seem to work at all...because when I do this:



f1 = foo
f1.a



...the result is not 0 as expected, but rather a runtime error.

rdg
09-29-2005, 05:45 PM
try

f1=foo()


if you use:

f1=foo
this means you want f1 to be a reference of foo.
Wich is handy if you have foo_1 and foo_2 and foo_3 and want to switch between them before executing on of their functions, like:

if a==b then executor=foo_1 else executor=foo_2
executor()


if you use

f1=foo()

you want to f1 to be the result of foo.
In your case returning foo with undefined a, b and c.

see also the OOP thread:
http://forums.cgsociety.org/showthread.php?t=272425&highlight=object+oriented

Georg

stuh505
09-29-2005, 05:51 PM
thanks rdg

CGTalk Moderation
09-29-2005, 05:51 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.