X
PrevPrev Go to previous topic
NextNext Go to next topic
Last Post 29 Aug 2003 09:04 AM by  anon
Arguments in class definitions
 2 Replies
Sort:
You are not authorized to post a reply.
Author Messages

anon



New Member


Posts:
New Member


--
29 Aug 2003 09:04 AM
    I'm new to IDL and fairly new to Object Oriented programming. I'd like to be able to define a class that has variable size -- that is, different instances of the class can take up different amounts of memory. For example, suppose I wanted to create a class called Polynomial, with the object data being an array of the coefficients of a polynomial. I would want to be able to specify the length of that array for each instance I created with a call to the OBJ_NEW routine. That way I could have one instance of the class be a quadratic, and another be a cubic, etc. (note that this is not my actual problem). I was thinking that this might be accomplished by allowing an argument to be passed to the STRUCT__DEFINE procedure, such as PRO POLYNOMIAL__DEFINE, degree struct = { POLYNOMIAL, $ coeff: INTARR(degree) } END but this doesn't seem to work. Is this possible in IDL (or any other language)? Thanks, Jared G.

    Deleted User



    New Member


    Posts:
    New Member


    --
    15 Aug 2003 11:03 AM
    Yes arguments to classes are very common. The trick is to use pointers to dynamically allocate memory. The neat part is the code that uses this object dosn't have to know anything about pointers. Here is your polynomial example. ;********* BEGIN ************ Function Poly::Init, DEGREE=degree self.coeif = PTR_NEW(INTARR(degree)) Return, 1 End Function Poly::getCoeif coeifPtr = self.coeif return, *coeifPtr End Function Poly::getSizeOfCoeif Return, n_Elements(self->getCoeif()) End Pro Poly__Define struct = {poly, coeif:PTR_NEW()} End ;********* END ************ To create this object just call myPoly = OBJ_NEW('Poly, DEGREE=5) Print, myPoly->getSizeOfCoeif() Hope this helps, Michael Ficker

    Deleted User



    New Member


    Posts:
    New Member


    --
    29 Aug 2003 09:04 AM
    You should use a pointer in the __DEFINE routine and then use the INIT method (called automatically by using OBJ_NEW) with the 'degree' argument. The __DEFINE routine is automatically called by IDL when it comes accross an unknown structure in a statement such as: ; Initialise new polynomial ; p1 = {POLYNOMIAL} and is not called with arguments. Look at the examples in the OO documentation that comes with IDL and hopefully this will all become clear. Hope this helps. Cliff
    You are not authorized to post a reply.