LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Member function within a struct

Hi,
   I would like to know if there is any way to have a function defined within a struct(for labwindow)? For example, I have this struct, V3 which contains a 3 X 1 array, Vec[3]. Now i would like to perform a normalisation function on this vector. Is it possible to define a function such that any variables with type V3 is able to perform this function? This is somehow similar to C++ classes member and function. Thanks!
 
 
0 Kudos
Message 1 of 5
(3,348 Views)
What I understand that you're wanting to do is to declare a function inside of a struct that is available as a member of this struct to perform operations on member variables (specifically an array of three elements).  I'm guessing that in reference to C++, you are thinking of the functionality within a class to define a function that will use member variables native to each instance of the class as in the example.

class Foo {
    int x;
    int addThree( ) {
       x += 3;
    }
}

If I understand your question correctly, the simple answer is no.  Since LabWindows/CVI is based on the C programming language, the lack of object-oriented class structure in the C language inhibits the use of a function in a struct.  Please note that, similar to C++, if you define a struct instead of a class, you cannot define functions within it either.

That being said, it sounds like you are just wanting to perform an operation on the same type of struct.  Outside of the struct, you CAN define a function that performs an operation on struct members.  In the following example, you can see how the function will perform an operation on a class member.

struct V3 {
    double Vec[3];
} ;

double Average (struct V3 foo) {
    int i;
    double sum = 0;
    for (i=0; i<sizeof(foo.Vec)/sizeof(*foo.Vec); i++) {
        DebugPrintf("%d\n",i);
       sum += foo.Vec[i];
    }
    return (sum/i);
}

If I have misunderstood your question, please let me know. 

Hope this helps!

Andy McRorie
Applications Engineer
National Instruments
Thanks,

Andy McRorie
NI R&D
0 Kudos
Message 2 of 5
(3,318 Views)
Hi,
   Thanks for replying to this thread. Actually what I had in mind is something along C++ class and member function. Its like creating a variable that behaves like C++ within C programming. I read somewhere that this is possible by using function pointers, but I doi not have a clear idea of how to implement it.
 
    Anyone who know about this please do drop me a msg!! Thanks
 
evangel55
 
0 Kudos
Message 3 of 5
(3,300 Views)
Based on this post, it's a little more clear about what you're wanting to do.  If I understand correctly what you are wanting, the feature is available when using a C++ compiler in conjunction with C code.  Check out this link on the subject, which includes a discussion of what I think you are referring to.  http://developers.sun.com/prodtech/cc/articles/mixing.html
 
If I did not understand you correctly, please clarify further.  And, of course, if anyone else has any input on the matter, feel free to contribute.
 
Thanks,
Andy McRorie
Applications Engineer
National Instruments

Message Edited by AndrewMc on 08-21-2006 09:42 PM

Thanks,

Andy McRorie
NI R&D
Message 4 of 5
(3,295 Views)
Thanks a lot! It really helps in my coding. Thanks again!!!
0 Kudos
Message 5 of 5
(3,288 Views)