Class Method using STRUCT and FUNCREF ?

Hello,

I'd like to gather structures and their built-in functions (methods), like it's done in C++.

Indeed, I think it would be easier to maintain the code if we had the possibility to do so.

For example, to initialize the fields of a structure, one would do : Instance.init(45,55).

Below, I've tried to do it

#pragma rtGlobals=1     // Use modern global access method.


main()


function main()

    STRUCT test_struct PT
    PT.Initialisation(PT,50,55) //<<------------- Here
    print PT
end

///////////////////////////////////////////

Structure test_struct
    variable  a
    variable  b
    FUNCREF Init_a_b Initialisation
EndStructure

///////////////////////////////////////////

Function Init_a_b(instance_locale,C,D)
    STRUCT test_struct &instance_locale
    variable C,D
   
    instance_locale.a = C
    instance_locale.b = D  
End


As you can notice, this is not exactly what I'd like to do, as we are obliged to mention the object (structure) on which we act.

If I remember well, in C++ we have to use the THIS statement, which is not implemented in IGOR PRO.

So, my question is, Is there a workaround to write Object Oriented Code ?

Thank you in advance.


Nasser wrote:
...For example, to initialize the fields of a structure, one would do : Instance.init(45,55).

Below, I've tried to do it ...


I must admit no experience in C or variants to help. However, as a general note, I am confused by why you are using the initialization call where parameter values are input. I consider initialization to be more "static", and generally would go about this as follows:

#pragma ModuleName=MyModule

Static Structure MyStructure
     variable A
     variable B
     ....
EndStructure

Static Function Initialize(mystr)
     STRUCT MyStructure &mystr
     mystr.A = 3
     mystr.B = 4
     ...
End

Static Function ChangeParams(mystr,[A,B,...])
     STRUCT MyStructure &mystr
     mystr.A = IfParamIsDefault(A) ? 3 : A
     mystr.B = IfParamIsDefault(B) ? 4 : B
     ...
End

Function MyMainFunction()
     STRUCT MyStructure &mystr
     Initialize(mystr)
     ...
     ChangeParams(mystr,B=5)
     ...
End


--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAHuntsville
Hello,

Thank you for your answer. I didn't know this way to do. Because I'm trying to write a reusable code, It will certainly be helpful.

Best Regards
this in C++ is an implicit parameter. Igor has no implicit parameters so you would always have to pass the structure representing an object to each method.

Some other C++ features that you will not be able to emulate in Igor are constructors, destructors, function overloading and inheritance. This all adds up to the conclusion that trying to emulate C++ in Igor will not buy you much, if anything. It will add complexity without adding any expressiveness.

You can write reusable code in any language by making routines general and organizing them into modules. What C++ and other object-oriented languages add to that is inheritance. You can't emulate inheritance in Igor.

Perhaps someday Igor will have support for object-oriented programming but not now.

You can write an Igor XOP in C++ using the Igor XOP Toolkit.