Results 1 to 4 of 4

Thread: structs, pointer, static callback function

  1. #1
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default structs, pointer, static callback function

    I have a static callback function like so:

    Qt Code:
    1. void Coin3dScene::flythruCB(void *data, SoSensor *s)
    2. {
    3. MyFlythruData *locData = (MyFlythruData *)data;
    4. //etc...
    5. }
    To copy to clipboard, switch view to plain text mode 

    The "data" I pass to the function is a struct that references variables I need to access in the callback:

    Qt Code:
    1. typedef struct
    2. {
    3. float *flyTimeLen;
    4. bool *flyLoop;
    5. } MyFlythruData;
    To copy to clipboard, switch view to plain text mode 

    set-up like so:

    Qt Code:
    1. MyFlythruData *myflydata = new MyFlyData;
    2. myflydata->flyTimeLen = &FlyTime;
    3. myflydata->flyLoop = &FlyLoop;
    To copy to clipboard, switch view to plain text mode 


    My question: is it necessary to use pointers in the struct as I've done? The callback function is called 50 times/second; by passing pointers to variables rather than the variable itself, the function should operate more smoothly with less overhead, right?

    But on the other hand, since the callback receives a pointer to data rather than just "data"... it's unnecesary to use pointers for the variables inside the struct... if I don't otherwise need to. right?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: structs, pointer, static callback function

    No, you can allocate the data on stack. Using pointers is just an overhead here and also a possible memory leak. Remember that using pointers requires the memory to be allocated and for that to happen your process needs to be preemptied so that the OS can give it some memory.

  3. The following user says thank you to wysota for this useful post:

    vonCZ (2nd June 2008)

  4. #3
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: structs, pointer, static callback function

    thanks for the help Wysota.

    Another question: I've lotsa data in this struct now, that I'm passing to the static callback function. I also need to include a pointer in the struct to a member function... so I can call that function from within the callback. I've looked around on the web but haven't found a solution that fits my particular situation: so far I get various compile errors. I'm trying various things like this:

    //in header:

    Qt Code:
    1. class Coin3dWindow : public SoQtExaminerViewer {
    2.  
    3. private:
    4.  
    5. void fly0_stop(void);
    6.  
    7. typedef struct {
    8.  
    9. void (*flyStop)(void);
    10. // void (*Coin3dWindow::flyStop)();
    11.  
    12. } MyFlyData;
    13.  
    14. MyFlyData *myflydata0;
    15. .
    16. .
    17. .
    18. }
    To copy to clipboard, switch view to plain text mode 


    // then in a Coin3dWindow member function I do

    Qt Code:
    1. myflydata0 = new MyFlyData;
    2. myflydata0->flyStop = &fly0_stop;
    To copy to clipboard, switch view to plain text mode 

    this is where I get the compile error:

    error C2276: '&' : illegal operation on bound member function expression

    if I drop the '&'

    myflydata0->flyStop = fly0_stop;

    i get:

    error C3867: 'Model::fly0_stop' : function call missing argument list; use '&Model::fly0_stop' to create a pointer to a member
    error C2440: '=' : cannot convert from 'void (_thiscall Model::*)(void)' to 'void (_cdecl *)(void)'

    so if I change it to:

    Qt Code:
    1. myflydata0->flyStop = &Model::fly0_stop;
    To copy to clipboard, switch view to plain text mode 

    I just get that last error message (the C2440, but not the C3867 obviously). Anyhow, this is the way I've seen it explained on the web... but I'm not sure why it won't compile. Any ideas?

    BTW, fly0_stop(void) is currently empty except for a single qDebug("hit");.

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: structs, pointer, static callback function

    Quote Originally Posted by vonCZ View Post
    error C2440: '=' : cannot convert from 'void (_thiscall Model::*)(void)' to 'void (_cdecl *)(void)'
    That flyStop field was declared as a pointer to a function that has no parameter and doesn't return anything. You can't make it point to a method of Model class, unless you change its type to void (Model::*flyStop)(void).

Similar Threads

  1. QPSQL problem
    By LoneWolf in forum Installation and Deployment
    Replies: 60
    Last Post: 4th November 2009, 15:22
  2. QPSQL driver in windows
    By brevleq in forum Installation and Deployment
    Replies: 31
    Last Post: 14th December 2007, 13:57
  3. how to add static library into qmake
    By Namrata in forum Qt Tools
    Replies: 1
    Last Post: 20th November 2007, 18:33
  4. QString static callback function from CURL
    By tpf80 in forum Qt Programming
    Replies: 12
    Last Post: 16th May 2007, 21:47
  5. use qpsql
    By raphaelf in forum Installation and Deployment
    Replies: 34
    Last Post: 22nd August 2006, 13:52

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.