Re: overload init function in class/object ? [message #34988 is a reply to message #34986] |
Tue, 06 May 2003 12:55   |
Mark Hadfield
Messages: 783 Registered: May 1995
|
Senior Member |
|
|
"paul wisehart" <wisehart@runbox.com> wrote in message
news:slrnbbg37q.2t7.wisehart@stingray.mcst.ssai.biz...
> I was under the assumption that you could overload the
> init function of an object.
>
> However, it doesn't work for me.
>
> In the below example I get an error if I try:
> IDL>o = obj_new('obj')
>
> Yet this works:
> IDL>o = obj_new('obj','test')
>
> I wanna be able to overload my init functions!
> Please tell me I'm doing something wrong.
>
>
> ;--- obj__define.pro -------------------
>
> function obj::init
> compile_opt idl2
> return, 1
> end
> ;-----------------------
> function obj::init, p1
> compile_opt idl2
> print, p1
> return, 1
> end
> ...
You can't overload a function in this way in IDL. When IDL compiles your
obj__define.pro the second version of obj::init will *replace* the first.
However IDL allows optional parameters, so the way to handle this in IDL is
something like
function obj::init, p1
compile_opt idl2
if n_elements(p1) gt 0 then print, p1
return, 1
end
The exact form of the test for the presence of the p1 positional parameter
depends on what you want to do with it; "if n_elements(p1) gt 0" is
generally what you want for input parameters.
--
Mark Hadfield "Ka puwaha te tai nei, Hoea tatou"
m.hadfield@niwa.co.nz
National Institute for Water and Atmospheric Research (NIWA)
|
|
|