You are not logged in.

#1 2012-04-17 09:26:51

n0stradamus
Member
Registered: 2010-11-08
Posts: 94
Website

[SOLVED] Passing a non-static member function as a function pointer

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! smile

Last edited by n0stradamus (2012-04-24 22:59:47)

Offline

#2 2012-04-17 22:37:29

Franek
Member
Registered: 2010-05-16
Posts: 100

Re: [SOLVED] Passing a non-static member function as a function pointer

I would not know how to code it myself, but if you do not mind using a library and function objects rather than pointers, libsigc++ might be for you. Maybe have a look at mem_fun (I recommend jumping right to "Detailed description" in the middle of the page).

Offline

#3 2012-04-18 06:16:01

peterb
Member
Registered: 2011-10-05
Posts: 27

Re: [SOLVED] Passing a non-static member function as a function pointer

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

#4 2012-04-18 08:51:16

n0stradamus
Member
Registered: 2010-11-08
Posts: 94
Website

Re: [SOLVED] Passing a non-static member function as a function pointer

Franek wrote:

I would not know how to code it myself, but if you do not mind using a library and function objects rather than pointers, libsigc++ might be for you. Maybe have a look at mem_fun (I recommend jumping right to "Detailed description" in the middle of the page).

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 smile

Offline

#5 2012-04-18 18:00:18

tavianator
Member
From: Waterloo, ON, Canada
Registered: 2007-08-21
Posts: 859
Website

Re: [SOLVED] Passing a non-static member function as a function pointer

n0stradamus wrote:

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 smile

Sadly, you can't use functors as an argument to [n]ftw().  They must be literal function pointers.

Offline

#6 2012-04-24 06:40:32

wes
Member
Registered: 2011-03-05
Posts: 67

Re: [SOLVED] Passing a non-static member function as a function pointer

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

#7 2012-04-24 18:27:58

zorro
Member
Registered: 2011-11-18
Posts: 47

Re: [SOLVED] Passing a non-static member function as a function pointer

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

#8 2012-04-24 22:58:55

n0stradamus
Member
Registered: 2010-11-08
Posts: 94
Website

Re: [SOLVED] Passing a non-static member function as a function pointer

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

Board footer

Powered by FluxBB