473,785 Members | 2,737 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

passing a pointer to member function problem

Hi,
In order to use an external api call that requires a function pointer I
am currently creating static wrappers to call my objects functions.
I want to re-jig this so I only need 1 static wrapper function.
I would prefer to be able to pass the member function as a void* to the
static wrapper but I suspect this may not even be possible.
Another solution would be option 2 below but I can't figure out the
syntax to call obj->*pFn().

Thanks in advance for any suggestions / observations.

typedef void (*ExecFn)(void *);

void external_api_fn (ExecFn fn,void* p)
{
fn(p);
}

class Doit
{
public:
void fn1(void){print f("fn1\n");}
void fn2(void){print f("fn2\n");}
//current solution
static void static_wrapper1 (void *obj)
{
((Doit*)obj)->fn1();
}
static void static_wrapper2 (void *obj)
{
((Doit*)obj)->fn2();
}
//option 1
static void static_wrapper3 (void *fn)
{
//fn();
}
//option 2
void (Doit::*pFn)(vo id);
static void static_wrapper4 (void *obj)
{
//execute pFn - obj->*pFn - how to execute pFn for obj?
}
//--
Doit()
{
//what Im doing
external_api_fn (static_wrapper 1,this);
external_api_fn (static_wrapper 2,this);
//option 1 - cast fn1 to void?
//external_api_fn (static_wrapper 3,fn1);
//external_api_fn (static_wrapper 3,fn2);
//option 2
pFn = fn1;
external_api_fn (static_wrapper 4,this);
pFn = fn2;
external_api_fn (static_wrapper 4,this);
}
};

Jul 18 '06 #1
3 2621
dice wrote:
Hi,
In order to use an external api call that requires a function pointer I
am currently creating static wrappers to call my objects functions.
I want to re-jig this so I only need 1 static wrapper function.
The problem is that you need to pass the 'this' pointer to the function.
I would prefer to be able to pass the member function as a void* to the
static wrapper but I suspect this may not even be possible.
As a matter of fact, taking the address of a method is possible, but
wouldn't solve your problem, since you need to pass the 'this' pointer
as well (so you actually have to pass _two_ parameters to the function).
Another solution would be option 2 below but I can't figure out the
syntax to call obj->*pFn().
Of course. You don't have the 'this' pointer.
typedef void (*ExecFn)(void *);

void external_api_fn (ExecFn fn,void* p)
{
fn(p);
}

class Doit
{
public:
void fn1(void){print f("fn1\n");}
void fn2(void){print f("fn2\n");}
//current solution
static void static_wrapper1 (void *obj)
{
((Doit*)obj)->fn1();
}
static void static_wrapper2 (void *obj)
{
((Doit*)obj)->fn2();
}
//option 1
static void static_wrapper3 (void *fn)
{
//fn();
}
//option 2
void (Doit::*pFn)(vo id);
static void static_wrapper4 (void *obj)
{
//execute pFn - obj->*pFn - how to execute pFn for obj?
}
//--
Doit()
{
//what Im doing
external_api_fn (static_wrapper 1,this);
external_api_fn (static_wrapper 2,this);
//option 1 - cast fn1 to void?
//external_api_fn (static_wrapper 3,fn1);
//external_api_fn (static_wrapper 3,fn2);
//option 2
pFn = fn1;
external_api_fn (static_wrapper 4,this);
pFn = fn2;
external_api_fn (static_wrapper 4,this);
}
};
How about this:

#include <stdio.h>

typedef void (*ExecFn)(void) ;

void external_api_fn (ExecFn fn)
{
fn ();
}

class Doit
{
typedef void (Doit::*PtrToMe mberForWrapperT ype) (void);
private:
static Doit* ms_ThisForWrapp er;
static PtrToMemberForW rapperType ms_PtrMemberFun cForWrapper;

static void static_wrapper ()
{
// Invoke the member function of the static object.
(ms_ThisForWrap per->*ms_PtrMemberF uncForWrapper) ();
}

public:
void fn1(void) {printf("fn1\n" );}
void fn2(void) {printf("fn2\n" );}

void call_external_a pi_fn (PtrToMemberFor WrapperType pMemberFunc)
{
// Store away the this pointer.
ms_ThisForWrapp er = this;
ms_PtrMemberFun cForWrapper = pMemberFunc;
external_api_fn (&static_wrappe r);
}
};

Doit::PtrToMemb erForWrapperTyp e Doit::ms_PtrMem berFuncForWrapp er = 0;
Doit* Doit::ms_ThisFo rWrapper = 0;

int main ()
{
Doit MyDoit;
MyDoit.call_ext ernal_api_fn (Doit::fn1);

return 0;
}
I took the liberty to change the definition of ExecFn to a function that
takes no arguments, since your members fn1 and fn2 don't take arguments.
The above solution is not intended for use in multi-threaded
applications, since it uses static class members. If you want to use
this in a multi-threaded app, you have to introduce a map that stores
these pointers for each thread that uses the class.

Regards,
Stuart
Jul 18 '06 #2

Stuart Redmann wrote:
dice wrote:
Hi,
In order to use an external api call that requires a function pointer I
am currently creating static wrappers to call my objects functions.
I want to re-jig this so I only need 1 static wrapper function.

The problem is that you need to pass the 'this' pointer to the function.
I would prefer to be able to pass the member function as a void* to the
static wrapper but I suspect this may not even be possible.

As a matter of fact, taking the address of a method is possible, but
wouldn't solve your problem, since you need to pass the 'this' pointer
as well (so you actually have to pass _two_ parameters to the function).
yes - i wasn't quite thinking straight there, but see option 2 - this
is essentially the same thing, pass the 'this' pointer after setting a
function pointer in the object.
>
Another solution would be option 2 below but I can't figure out the
syntax to call obj->*pFn().

Of course. You don't have the 'this' pointer.
'this' is passed as the second arg as per:
external_api_fn (static_wrapper 4,this);

I thought that since the following is possible by passing 'this' as
void* obj:
((Doit*)obj)->fn1();
then static_wrapper4 could use something like the following:
(((Doit*)obj)->*pFn)();
>
typedef void (*ExecFn)(void *);

void external_api_fn (ExecFn fn,void* p)
{
fn(p);
}

class Doit
{
public:
void fn1(void){print f("fn1\n");}
void fn2(void){print f("fn2\n");}
//current solution
static void static_wrapper1 (void *obj)
{
((Doit*)obj)->fn1();
}
static void static_wrapper2 (void *obj)
{
((Doit*)obj)->fn2();
}
//option 1
static void static_wrapper3 (void *fn)
{
//fn();
}
//option 2
void (Doit::*pFn)(vo id);
static void static_wrapper4 (void *obj)
{
//execute pFn - obj->*pFn - how to execute pFn for obj?
}
//--
Doit()
{
//what Im doing
external_api_fn (static_wrapper 1,this);
external_api_fn (static_wrapper 2,this);
//option 1 - cast fn1 to void?
//external_api_fn (static_wrapper 3,fn1);
//external_api_fn (static_wrapper 3,fn2);
//option 2
pFn = fn1;
external_api_fn (static_wrapper 4,this);
pFn = fn2;
external_api_fn (static_wrapper 4,this);
}
};

How about this:

#include <stdio.h>

typedef void (*ExecFn)(void) ;

void external_api_fn (ExecFn fn)
{
fn ();
}

class Doit
{
typedef void (Doit::*PtrToMe mberForWrapperT ype) (void);
private:
static Doit* ms_ThisForWrapp er;
static PtrToMemberForW rapperType ms_PtrMemberFun cForWrapper;

static void static_wrapper ()
{
// Invoke the member function of the static object.
(ms_ThisForWrap per->*ms_PtrMemberF uncForWrapper) ();
}

public:
void fn1(void) {printf("fn1\n" );}
void fn2(void) {printf("fn2\n" );}

void call_external_a pi_fn (PtrToMemberFor WrapperType pMemberFunc)
{
// Store away the this pointer.
ms_ThisForWrapp er = this;
ms_PtrMemberFun cForWrapper = pMemberFunc;
external_api_fn (&static_wrappe r);
}
};

Doit::PtrToMemb erForWrapperTyp e Doit::ms_PtrMem berFuncForWrapp er = 0;
Doit* Doit::ms_ThisFo rWrapper = 0;

int main ()
{
Doit MyDoit;
MyDoit.call_ext ernal_api_fn (Doit::fn1);
this builds and works on my evC++ compiler, but not with mingw32-g++
>
return 0;
}
I took the liberty to change the definition of ExecFn to a function that
takes no arguments, since your members fn1 and fn2 don't take arguments.
The above solution is not intended for use in multi-threaded
applications, since it uses static class members. If you want to use
Multi threading is not a problem in this particular instance, but I was
trying to avoid static members for just this reason.
this in a multi-threaded app, you have to introduce a map that stores
these pointers for each thread that uses the class.

Regards,
Stuart
Jul 18 '06 #3
dice wrote:
Stuart Redmann wrote:
>>dice wrote:
>>>Hi,
In order to use an external api call that requires a function pointer I
am currently creating static wrappers to call my objects functions.
I want to re-jig this so I only need 1 static wrapper function.
I would prefer to be able to pass the member function as a void* to the
static wrapper but I suspect this may not even be possible.
Another solution would be option 2 below but I can't figure out the
syntax to call obj->*pFn().
I think I misunderstood you here. Invokation of member functions is
quite ugly (I had to look it up myself). If you have the object and the
pointer to the member function at hand, you can invoke it with the above
syntax, e.g. obj->*pFn (), but you have to enclose this in parentheses:
(obj->*pFn) ()
>>>typedef void (*ExecFn)(void *);

void external_api_fn (ExecFn fn,void* p)
{
fn(p);
}
A even much simpler solution comes into my mind: Since your API function
can be passed _any_ pointer, why don't we give it a pointer to a struct
containing both the object pointer and the member function pointer?
Something like this:

#include <stdio.h>

typedef void (*ExecFn)(void* );

void external_api_fn (ExecFn fn, void* p)
{
fn (p);
}

class Doit
{
typedef void (Doit::*PtrToMe mberForWrapperT ype) (void);
struct SApiFunctionPar ameters
{
Doit* m_pThisPointer;
PtrToMemberForW rapperType m_pMemberFuncti on;
};

private:
static void static_wrapper (void* p_VoidPointer)
{
// Cast the passed pointer to a pointer to a parameter struct.
SApiFunctionPar ameters* pParameters = (SApiFunctionPa rameters*)
p_VoidPointer;

// Invoke the passed member function for the passed 'this' pointer.
(pParameters->m_pThisPoint er->*pParameters->m_pMemberFunct ion) ();
}

public:
void fn1(void) {printf("fn1\n" );}
void fn2(void) {printf("fn2\n" );}

void call_external_a pi_fn (PtrToMemberFor WrapperType pMemberFunc)
{
// Store away the 'this' AND the member function pointer.
SApiFunctionPar ameters ApiFunctionPara meters;
ApiFunctionPara meters.m_pThisP ointer = this;
ApiFunctionPara meters.m_pMembe rFunction = pMemberFunc;
external_api_fn (&static_wrappe r, &ApiFunctionPar ameters);
}
};

int main ()
{
Doit MyDoit;
MyDoit.call_ext ernal_api_fn (Doit::fn1);

return 0;
}

[snipped previous solution]
>
this builds and works on my evC++ compiler, but not with mingw32-g++
(*blush*) I work with Visual C 6.0 (at least I have the courage to admit
it). Sorry, I can't help you there.
>
Multi threading is not a problem in this particular instance, but I was
trying to avoid static members for just this reason.
Luckily, multi-threading will be no problem when you use the struct version.

Regards,
Stuart
Jul 19 '06 #4

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

Similar topics

5
9377
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1, 2, func);
58
10181
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
6
8830
by: keepyourstupidspam | last post by:
Hi, I want to pass a function pointer that is a class member. This is the fn I want to pass the function pointer into: int Scheduler::Add(const unsigned long timeout, void* pFunction, void* pParam)
17
2813
by: Christopher Benson-Manica | last post by:
Does the following program exhibit undefined behavior? Specifically, does passing a struct by value cause undefined behavior if that struct has as a member a pointer that has been passed to free()? #include <stdlib.h> struct stype { int *foo; };
7
2643
by: Jake Thompson | last post by:
Hello I created a DLL that has a function that is called from my main c program. In my exe I first get a a pointer to the address of the function by using GetProcAddress and on the dll side I make sure that my function is being exported by adding a line to the .def file. This seems to work because when I debug it recognizes the dll and once it hits the function it goes right into the proper location of the dll.
11
4432
by: cps | last post by:
Hi, I'm a C programmer taking my first steps into the world of C++. I'm currently developing a C++ 3D graphics application using GLUT (OpenGL Utility Toolkit written in C) for the GUI components. The application is built around a "World" object that contains a "GUI" object that is a C++ wrapper around GLUT. What I'd like to do is pass a pointer to a member function in the World class to function in the GUI
7
3307
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the object is a reference type? my code is not proving that. I have a web project i created from a web service that is my object: public class ExcelService : SoapHttpClientProtocol {
1
3978
by: autumn | last post by:
Hi everybody, I'm having problem passing pointer to member object as template argument, seems VC 2005 does not allow 'pointer to base member' to 'pointer to derived member' conversion in template arguments, is this VC specific or a standard c++ behavior? the code looks like this: class Base { public: int member; };
8
3504
by: S. | last post by:
Hi all, Can someone please help me with this? I have the following struct: typedef struct { char *name; int age; } Student;
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10090
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9949
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8971
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.