Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / November 2004

Tip: Looking for answers? Try searching our database.

Non-static member function as a callback

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Agoston Bejo - 23 Nov 2004 20:34 GMT
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
Steve Friedl [MVP] - 23 Nov 2004 22:59 GMT
> I would like to do something like this:
>
[quoted text clipped - 9 lines]
>     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
Arnaud Debaene - 23 Nov 2004 23:18 GMT
> Hi,
> I would like to do something like this:
[quoted text clipped - 12 lines]
>
> 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
Agoston Bejo - 24 Nov 2004 19:44 GMT
> > Hi,
> > I would like to do something like this:
[quoted text clipped - 40 lines]
>    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
Arnaud Debaene - 24 Nov 2004 23:13 GMT
> Yes, I was thinking about something similar. The problem is that I
> haven't used boost for a while.
[quoted text clipped - 11 lines]
> 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=&threadm=%23vK9x6wVEHA.2992%40TK2MSFTNGP12.
phx.gbl&rnum=3&prev=/groups%3Fas_q%3DATL%2520thunk%26as_uauthors%3DArnaud%2520De
baene%26lr%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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.