473,396 Members | 2,036 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Non-static member function as a callback

Hi,
I would like to do something like this:
struct A {
int f(float f);
};
....

int g(int(*f1)(float)) { return f1(6.5); }

int main() {
A a;
g(a.*f); // or whatever. This won't compile, of course
}
Is this possible? If so, then how?

Thx,
Gus
Nov 17 '05 #1
4 1589
"Agoston Bejo" wrote:
I would like to do something like this:

struct A {
int f(float f);
};
....

int g(int(*f1)(float)) { return f1(6.5); }

int main() {
A a;
g(a.*f); // or whatever. This won't compile, of course
}


I don't see how this could work: all non-static class methods require a
"this" pointer, and though it's possible to do it in the specific case where
the *recipient* function knows it's a class-based callback, for the general
case of "we just need a callback", there would be no "this" pointer.

I suspect that for very simple classes with no virtual anything, one could
force it with a cast, hoping that the member function didn't try to touch its
own object data, but (a) that would be a hideous, nonportable atrocity not
ever to be countenanced, and (b) if you don't need the member data, then it
might as well be static.

Steve
Nov 17 '05 #2
Agoston Bejo wrote:
Hi,
I would like to do something like this:
struct A {
int f(float f);
};
...

int g(int(*f1)(float)) { return f1(6.5); }

int main() {
A a;
g(a.*f); // or whatever. This won't compile, of course
}
Is this possible? If so, then how?


As steve as explianed, a member function pointer is not a simple function
pointer because :
- the member function takes an additrionnal, hidden, "this" parameter.
- the pointer itself may need to embed more information that just the
function address, in order to be able to call the right override in case of
virtual function. That's why the effective size of a member function pointer
can vary from 4 to 16 bytes, depending on the class hierarchy.

One possible solution to your problem is to replace your simple function
pointer in g by a template parameter, and then use boost::bind to create a
nullary functor calling your objec tinstance member function :

struct A
{
int f(float val);
};

template <typename FunctorType> int g(FunctorType functor)
{
return functor(6.5);
}

int main
{
A a;
g (boost::bind(&A::f, a));
}

See www.boost.org for details.

Arnaud
MVP - VC
Nov 17 '05 #3

"Arnaud Debaene" <ad******@club-internet.fr> wrote in message
news:uL**************@tk2msftngp13.phx.gbl...
Agoston Bejo wrote:
Hi,
I would like to do something like this:
struct A {
int f(float f);
};
...

int g(int(*f1)(float)) { return f1(6.5); }

int main() {
A a;
g(a.*f); // or whatever. This won't compile, of course
}
Is this possible? If so, then how?
As steve as explianed, a member function pointer is not a simple function
pointer because :
- the member function takes an additrionnal, hidden, "this" parameter.
- the pointer itself may need to embed more information that just the
function address, in order to be able to call the right override in case

of virtual function. That's why the effective size of a member function pointer can vary from 4 to 16 bytes, depending on the class hierarchy.

One possible solution to your problem is to replace your simple function
pointer in g by a template parameter, and then use boost::bind to create a
nullary functor calling your objec tinstance member function :

struct A
{
int f(float val);
};

template <typename FunctorType> int g(FunctorType functor)
{
return functor(6.5);
}

int main
{
A a;
g (boost::bind(&A::f, a));
}
Yes, I was thinking about something similar. The problem is that I haven't
used boost for a while.
Actually, is there a way to pass
boost::bind(&A::f, a)
as a function pointer?

E.g. if g()'s signature stays as originally was, can this be passed to it?
Moreover, can it be assigned to a function pointer?
I.e.

int (*fp)(float);
fp = bind(&A::f, a); // this won't compile.

Is there maybe a function in boost that converts bind(&A::f, a) into an
old-style function pointer?

The problem behind this whole thing is that I would like to use objects for
representing windows.
Every object would have a WinProc callback function. Of course the old
WinAPI knows nothing about functors and such, but still, I would like to do
it that way if it's possible.

See www.boost.org for details.

Arnaud
MVP - VC

Nov 17 '05 #4
Agoston Bejo wrote:

Yes, I was thinking about something similar. The problem is that I
haven't used boost for a while.
Actually, is there a way to pass
boost::bind(&A::f, a)
as a function pointer?

E.g. if g()'s signature stays as originally was, can this be passed
to it? Moreover, can it be assigned to a function pointer?
I.e.

int (*fp)(float);
fp = bind(&A::f, a); // this won't compile.

Is there maybe a function in boost that converts bind(&A::f, a) into
an old-style function pointer? No!! As I have explained, the "binder" is alays bigger that a mere function
pointer because it must contains :
- a pointer to the a object (32 bits)
- An object-member function pointer, which can be 32 to 128 bits, depending
on the base classe(s) of A, wether f is virtual, etc....
All of this could never be "held" in a 32 bits flat function pointer
The problem behind this whole thing is that I would like to use
objects for representing windows. Ah! And Yet Another Window Object Oriented Framework (YAWOOF) ! ;-)
Every object would have a WinProc callback function. Of course the old
WinAPI knows nothing about functors and such, but still, I would like
to do it that way if it's possible.

There are several solutions that have already been discutted here. See
http://www.google.fr/groups?hl=fr&lr...r%3D%26hl%3Dfr
for example.
Basically, there seems to be 3 possibilites (maybe I've forgotten others,
anyone???) :
- using the Window user data (GetWindowLong/SetWindowLong, or better
GetWindowLongPtr/SetWindowLongPtr) to hold a pointer to the C++ "window"
object.
- having a global map HWND<-> C++ object pointer.
- Using a thunk (dynamically allocated and written assembly code) to
subclass the window and "hack" the call stack to replace the HWND parameter
to WndProc by a pointer to the C++ corresponding object.

Arnaud
MVP - VC
Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: lothar | last post by:
re: 4.2.1 Regular Expression Syntax http://docs.python.org/lib/re-syntax.html *?, +?, ?? Adding "?" after the qualifier makes it perform the match in non-greedy or minimal fashion; as few...
3
by: Mario | last post by:
Hello, I couldn't find a solution to the following problem (tried google and dejanews), maybe I'm using the wrong keywords? Is there a way to open a file (a linux fifo pipe actually) in...
25
by: Yves Glodt | last post by:
Hello, if I do this: for row in sqlsth: ________pkcolumns.append(row.strip()) ________etc without a prior:
4
by: bwmiller16 | last post by:
Guys - I'm doing a database consistency check for a client and I find that they're building unique indexes for performance/query reasons where they could be using non-unique indexes. Note...
4
by: Dave | last post by:
I need to add the ability to drag from a Windows Form and drop into a non dotNet application. For example, having a generated image in my app that I wish to drag out into explorer as a friendly way...
0
by: Christopher Attard | last post by:
Hi, I need to create a dialog like the 'Add Counters' dialog box in perfmon. I'm using the System.Diagnostics namespace class in .NET and I've managed to do it. The problem arises when I'm...
8
by: John Hazen | last post by:
I want to match one or two instances of a pattern in a string. According to the docs for the 're' module ( http://python.org/doc/current/lib/re-syntax.html ) the '?' qualifier is greedy by...
0
by: amitvps | last post by:
Secure Socket Layer is very important and useful for any web application but it brings some problems too with itself. Handling navigation between secure and non-secure pages is one of the cumbersome...
399
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
12
by: puzzlecracker | last post by:
is it even possible or/and there is a better alternative to accept input in a nonblocking manner?
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.