LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Passing an object to a callback

I would like to have access to a C++ object from within my timer callback function without making the object global.  Example below:

 

in 'MyClass.h'

 

class MyClass

{

public:

     MyClass();

     ~MyClass();

 

     void MyClassFunction();

 

}

 

in 'MyClass.cpp'

 

MyClass::MyClass()

{

    //do some constructor stuff here

}

MyClass::~MyClass()

{

    //Some destructor stuff here

}

 

void MyClassFunction()

{

    //some function stuff here

}

 

in 'Display.cpp'

 

 

//This is called periodically at a fixed time interval

int CVICALLBACK displayTick (int panel, int control, int my_event,
                         void *callbackData, int eventData1, int eventData2)
{

    //I want access to an instantiation of MyClass here, without making the instantiation global....

    MyClass_object.MyClassFunction(); //do some stuff using my object here

}

 

There's probably a simple solution to this, but I've never done it.

 

0 Kudos
Message 1 of 3
(2,765 Views)

Hello JKelly662,

 

An instantiation of a class is also referred to as an object. Let's back up a little bit from there and put this in terms of something you are probably already familiar with: variables.

 

Variables have a type (int, char, etc.), and you can declare instances of these variables. For example:

int x = 0;

int y = 0;

Here, int is the type and x and y are instantiations of the type (given the value 0).You can think of this almost as if int is a class and x and y are objects (instantiations) of this class.

 

Continuing forward, you can declare multiple objects from the same class, and public members (like what you specified) are accessible anywhere the object is visible. 

 

All that being said, if you include MyClass.h in Display.cpp, you could create an object of your 'MyClass' class in Display.cpp. Having this object in Display.cpp would allow you to access the members of that object in your callback that is in Display.cpp

 

Here's some additional information that I think may be helpful: http://www.cplusplus.com/doc/tutorial/classes/

 

Regards,

Will

0 Kudos
Message 2 of 3
(2,704 Views)

Basically during initialization of your program you want to somewhere do something like:

 

static int panelHandle;

int main (int argc, char *argv[]) { int status; if (InitCVIRTE (0, argv, 0) == 0) return -1; /* out of memory */ if ((panelHandle = LoadPanel (0, "sample.uir", PANEL)) < 0) return -1;
MyClass *instance = new MyClass(); ............ status = SetCtrlAttribute(panelHandle, TIMER_CTRL_ID , ATTR_CALLBACK_DATA, instance); ............ DisplayPanel (panelHandle);
status = RunUserInterface ();
DiscardPanel(panelHandle);
return 0; }
//This is called periodically at a fixed time interval int CVICALLBACK displayTick (int panel, int control, int my_event,                          void *callbackData, int eventData1, int eventData2) { MyClass *instance = (MyClass)callbackData;     //I want access to an instantiation of MyClass here, without making the instantiation global....     instance->MyClassFunction(); //do some stuff using my object here }

 

Rolf Kalbermatter
My Blog
0 Kudos
Message 3 of 3
(2,677 Views)