PDA

View Full Version : static declaration



jhearon
18th February 2008, 17:49
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.



model.h

static SoDragPointDragger ** draggers = NULL;
...
class Model : public SoQtExaminerViewer
{

public:

Model( QWidget *parent = NULL, const char *name=NULL, const SbBool embed=TRUE);
~Model(void);

struct dragcb_data;
static void dragger_moved(void * data, SoDragger * dragger);
void setDefaultScene();
...

-----------
model.cpp
#include <model.h>

Model::Model( QWidget *parent, const char * name, const SbBool embed)
: SoQtExaminerViewer( parent, name, embed, SoQtFullViewer::BUILD_ALL, SoQtViewer::BROWSER, TRUE)
{
...
struct Model::dragcb_data {
unsigned int idx;
};

void Model::dragger_moved(void * data, SoDragger * dragger)
{
SoDragPointDragger * dpd = (SoDragPointDragger *)dragger;
struct dragcb_data * cbdata = (struct dragcb_data *)data;
SbVec3f v = dpd->translation.getValue();
ctrlpts->point.set1Value(cbdata->idx, v[0], v[1], v[2]);
}

void Model::setDefaultScene(void) {

...
struct dragcb_data * data = new struct dragcb_data;
data->idx = i;
dpd->addMotionCallback(dragger_moved, data);
dpd->translation.setValue(pts[i]);
root->addChild(dpd);
...
}

jacek
18th February 2008, 21:37
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.