[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: do I really need to use loops on objects?
> Brad Gom wrote:
> > It seems odd to me that I can't treat object arrays with the same
> > elegance that is possible with all other array types. If I have a large
> > list of objects of the same type, and I want to call the same method on
> > each of them, do I really have to use a for loop? Wouldn't it be more
> > consistent with the IDL philosophy to write: object_array->method()
> > instead of: for i=0,10 do object_array[i]->method()
> I agree completely, but most people on this group don't appear to.
> The counter argument seems to be that an object array can contain
> different kinds of objects, so you can't be sure that any particular
> method will work for every element of the array. While true, this
> seems (to me) to be a reason why object arrays should contain only
> similar elements *JUST LIKE EVERY OTHER IDL ARRAY TYPE*.
Uhhh, how about pointer arrays? Think of Object arrays as object *pointer*
arrays, as in C++, and it will become more intuitive. You want to call a method
on an array of objects of the same class? Fine. Make them data members of
another object which does the dirty work for you, including enforcing the single
class requirement (see obj_isa())!
Since IDL would just be doing a loop anyway (you can't optimize polymorphism),
this is just as convenient. I too at first felt the way you do, but then I
began to realize their point.
E.g.: With this superclass, you could then simply say
and have Method5 called on 50 objects at a time. You could make the superclass
quite general, if you want (e.g. every class you ever write has a "Print"
method...). When it comes to object oriented programming, to quote from Dune, ,
"you must bend like a reed in the wind."
J.D. Smith |*| WORK: (607) 255-5842
Cornell University Dept. of Astronomy |*| (607) 255-6263
304 Space Sciences Bldg. |*| FAX: (607) 255-5875
Ithaca, NY 14853 |*|