Results 1 to 2 of 2

Thread: static declaration

  1. #1
    Join Date
    Jun 2007
    Posts
    25
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default static declaration

    Hi
    I'm working on a QT Coin/SoQt widget. I'm confused about a static hack to the widget part I made which works, but I don't understand why. Doesn't seem right, but it works. I'm trying to do it the correct way. Hoping to get some advice. Sorry if this is a bit O.T. since QT part is not shown.

    The problem is with the function dragger_moved.


    If I declare the function dragger_moved in .h as
    void dragger_moved(void * data, SoDragger * dragger);
    as non-static, and have the .cpp function also as non-static
    void Model::dragger_moved(void*, SoDragger*) I get an error.
    "error: no matching function for call to 'SoDragPointDragger::addMotionCallback"

    But if I declare the function dragger_moved in .h as static:
    static void dragger_moved(void * data, SoDragger * dragger);
    and also have the .cpp function as static:
    static void Model::dragger_moved(void*, SoDragger*)
    I get a different error:
    "error: cannot declare member function 'static void Model::dragger_moved(void*, SoDragger*)' to have static linkage"

    I came up with a hack of declaring the function dragger_moved in .h as static
    static void dragger_moved(void * data, SoDragger * dragger);
    but using non-static in the .cpp function as
    void Model::dragger_moved(void*, SoDragger*)
    and this gets thru the compiler.

    That doesn't seem the best way to do it. Ideally the dragger_moved function should be static since it deals with the widget's scene graph.

    Qt Code:
    1. model.h
    2.  
    3. static SoDragPointDragger ** draggers = NULL;
    4. ...
    5. class Model : public SoQtExaminerViewer
    6. {
    7.  
    8. public:
    9.  
    10. Model( QWidget *parent = NULL, const char *name=NULL, const SbBool embed=TRUE);
    11. ~Model(void);
    12.  
    13. struct dragcb_data;
    14. static void dragger_moved(void * data, SoDragger * dragger);
    15. void setDefaultScene();
    16. ...
    17.  
    18. -----------
    19. model.cpp
    20. #include <model.h>
    21.  
    22. Model::Model( QWidget *parent, const char * name, const SbBool embed)
    23. : SoQtExaminerViewer( parent, name, embed, SoQtFullViewer::BUILD_ALL, SoQtViewer::BROWSER, TRUE)
    24. {
    25. ...
    26. struct Model::dragcb_data {
    27. unsigned int idx;
    28. };
    29.  
    30. void Model::dragger_moved(void * data, SoDragger * dragger)
    31. {
    32. SoDragPointDragger * dpd = (SoDragPointDragger *)dragger;
    33. struct dragcb_data * cbdata = (struct dragcb_data *)data;
    34. SbVec3f v = dpd->translation.getValue();
    35. ctrlpts->point.set1Value(cbdata->idx, v[0], v[1], v[2]);
    36. }
    37.  
    38. void Model::setDefaultScene(void) {
    39.  
    40. ...
    41. struct dragcb_data * data = new struct dragcb_data;
    42. data->idx = i;
    43. dpd->addMotionCallback(dragger_moved, data);
    44. dpd->translation.setValue(pts[i]);
    45. root->addChild(dpd);
    46. ...
    47. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    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: static declaration

    Quote Originally Posted by jhearon View Post
    I came up with a hack of declaring the function dragger_moved in .h as static
    static void dragger_moved(void * data, SoDragger * dragger);
    but using non-static in the .cpp function as
    void Model::dragger_moved(void*, SoDragger*)
    and this gets thru the compiler.
    It's not a hack. Just dragger_moved() isn't a function, but a method of Model class and static method != static function.

    If you declare a method as static (by adding static in front of its declaration in .h file), it will belong to class's namespace, but it won't operate on class's instance like normal methods.

    While static functions (you declare them by adding static in front of the function's definitions .cpp file) are functions that are visible only within current compilation unit.

Similar Threads

  1. Replies: 16
    Last Post: 23rd May 2008, 10:12
  2. Windows XP Qt 4.3.2 Static build
    By maxpower in forum Installation and Deployment
    Replies: 9
    Last Post: 2nd November 2007, 04:49
  3. Replies: 2
    Last Post: 16th March 2007, 09:04
  4. Accessing to a static variable from the same class
    By xgoan in forum General Programming
    Replies: 6
    Last Post: 5th March 2007, 10:50
  5. Replies: 4
    Last Post: 14th February 2006, 21:35

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.