You are not logged in.
I need to pass a function pointer to a public method to a system call, nftw() to be precise.
I know that member functions don't match the required signature because of the hidden 'this' pointer, but
the only way to work around that is by using a small wrapper function that makes use of a global variable (the object of which I want to call the method).
Speaking in code, this is the way I've solved the problem currently:
// create a global variable here
static MyObject obj;
static int myObject_method_wrapper(const char *fpath, const struct stat *sb, int tflag, struct FTW *ftwbuf) {
return obj.handleDirEntry(fpath, sb, tflag, ftwbuf);
}
// somewhere in main()
nftw(walkroot, myObject_method_wrapper, 20, FTW_PHYS);
Now, my question: Can't this be done without a global instance of MyObject? It is pointed out here that other ways are existent, but sadly they are not mentioned.
Glad if someone could help me out!
Last edited by n0stradamus (2012-04-24 22:59:47)
Offline
Offline
Spontaneously i can only think of this (not really elegant solution) of using a static pointer variable:
// Foo.h
class Foo {
public:
Foo();
static void Bar();
static Foo* This;
void Baz();
}
// Foo.cpp
#include "Foo.h"
Foo *Foo::This = 0;
Foo::Foo() {
Foo::This = this;
}
void Foo::Bar() {
This->Baz();
}
where Bar() is the static method you want to pass as a pointer and Baz() is the instance method you want to execute.
Offline
I have not worked with function objects before, so that will require some work on my part. But the docs look good and when
I've understood what functors/function objects are, it might just work
Offline
I have not worked with function objects before, so that will require some work on my part. But the docs look good and when
I've understood what functors/function objects are, it might just work
Sadly, you can't use functors as an argument to [n]ftw(). They must be literal function pointers.
Offline
I think you are stuck:
1. You are not in control of the interface (of nftw), and furthermore,
2. You are not in control of any of the parameters sent to the callback.
nftw has no idea which one of your objects it is supposed to reference, and
there's no apparent way to tell it.
But given this situation, are you sure it makes sense to use a non-static
member? It seems kind of strange to me-- any instance-specific data is
necessarily going to be independent of the function calls! So even if you
engineer something to avoid using a global, whatever you engineer is still
going to involve some *arbitrary* instance of your class (e.g. peterb's
solution, which uses the most recently created instance). The arbitrary-ness
doesn't feel right, since it sort of implicitly says that none of the instance
data is important. No important instance-specific data sounds like static...
Offline
As Wes stated, you are restricted to the interface provided by nftw. But you can hide this fact deep within a class by declaring a private static method and static data. When a non-static public method is invoked, it can use these statics as temporaries.
This is very close to the code snippet from PeterB but that statics are private and the 'This' pointer is set each time the public method is invoked.
Last edited by zorro (2012-04-24 18:39:23)
Offline
Until now I solved the problem like in my first post. It works and it is simple enough that every reader understands it.
@wes: I want to use a member function, that is non-static, because the behaviour of said function is dependant on
some instance-specific values. Also, I'm using a C++-library, so I don't want to mix programming paradigms
as I want to improve on code-cleanliness.
Thanks for all the answers!
I'd be happy, if you could help me with this problem, concerning C++ IO.
Offline