Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > static member functions vs. member function pointers

Reply
Thread Tools

static member functions vs. member function pointers

 
 
paul
Guest
Posts: n/a
 
      04-29-2009
Hi,
I have seen the use of static member functions for callbacks by
passing the name of the static member function and the this pointer to
the code that will call back.
I was thinking the same can be achieved by using member function
pointers and the this pointer.

E.g.
void FuncThatWillCallback( MyClass::staticFunc, (void *)this)
{
staticFunc(this);
}
void FuncThatWillCallback( PtrToMemberFunc, (void *)this)
{
this->PtrToMemberFunc;
}

Is there a reason why the static member function trick is so widely
used? (Except when FuncThatWillCallback is part of a 3rd party lib).

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      04-29-2009
* paul:
> Hi,
> I have seen the use of static member functions for callbacks by
> passing the name of the static member function and the this pointer to
> the code that will call back.
> I was thinking the same can be achieved by using member function
> pointers and the this pointer.
>
> E.g.
> void FuncThatWillCallback( MyClass::staticFunc, (void *)this)
> {
> staticFunc(this);
> }
> void FuncThatWillCallback( PtrToMemberFunc, (void *)this)
> {
> this->PtrToMemberFunc;
> }


Have you tried compiling any of the above?


> Is there a reason why the static member function trick is so widely
> used? (Except when FuncThatWillCallback is part of a 3rd party lib).
>


Do you have any data that supports your contention that static member functions
are widely used for callbacks in code that doesn't involve 3rd party libraries?


Cheers, & hope these question help,

- Alf

--
Due to hosting requirements I need visits to <url: http://alfps.izfree.com/>.
No ads, and there is some C++ stuff! Just going there is good. Linking
to it is even better! Thanks in advance!
 
Reply With Quote
 
 
 
 
Vladimir Jovic
Guest
Posts: n/a
 
      04-29-2009
Best to post real code

paul wrote:
> Hi,
> I have seen the use of static member functions for callbacks by
> passing the name of the static member function and the this pointer to
> the code that will call back.
> I was thinking the same can be achieved by using member function
> pointers and the this pointer.


You don't need this pointer, but you have to pass the object of the
class type for which you want to create a callback to a member function.

>
> E.g.
> void FuncThatWillCallback( MyClass::staticFunc, (void *)this)
> {
> staticFunc(this);
> }
> void FuncThatWillCallback( PtrToMemberFunc, (void *)this)
> {
> this->PtrToMemberFunc;
> }
>


This doesn't look like a legal c++. You didn't pass parameters when you
called the member function. And, can you call the member function on a
void pointer?

> Is there a reason why the static member function trick is so widely
> used? (Except when FuncThatWillCallback is part of a 3rd party lib).
>


"Widely used" is so vague term.
 
Reply With Quote
 
DerTopper@web.de
Guest
Posts: n/a
 
      04-29-2009
paul wrote:
> I have seen the use of static member functions for callbacks by
> passing the name of the static member function and the this pointer to
> the code that will call back.
> I was thinking the same can be achieved by using member function
> pointers and the this pointer.
> E.g.
> void FuncThatWillCallback( MyClass::staticFunc, (void *)this)
> {
> staticFunc(this);
> }
> void FuncThatWillCallback( PtrToMemberFunc, (void *)this)
> {
> this->PtrToMemberFunc;
> }
>
> Is there a reason why the static member function trick is so widely
> used? (Except when FuncThatWillCallback is part of a 3rd party lib).


Most probably we are talking about APIs that are actually C APIs, like
the most of MS's Windows API. There you can create threads by passing
a pointer to a C function to the CreateThread function. If you want to
use some object to service the thread, you usually pass the 'this'
pointer to the thread function.

Note that this API is designed for C, so that it doesn't know how to
handle member pointers.

Of course, if you want to define our own callback mechanism, you
should rather make it interface based:
class ICallableObject
{
public:
virtual void DoSomething (/* some args*/) = 0;
};

void FuncThatWillCallback (ICallableObject* pCallableObject)
{
pCallableObject->DoSomething (/* args */);
}

This is much cleaner that the C version, but can only be used under C+
+.

Regards,
Stuart
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      04-29-2009
On Apr 29, 8:41 am, paul <(E-Mail Removed)> wrote:

> I have seen the use of static member functions for callbacks
> by passing the name of the static member function and the this
> pointer to the code that will call back.


That's interesting. I've never seen it, at least not in correct
code.

> I was thinking the same can be achieved by using member
> function pointers and the this pointer.


> E.g.
> void FuncThatWillCallback( MyClass::staticFunc, (void *)this)
> {
> staticFunc(this);}


> void FuncThatWillCallback( PtrToMemberFunc, (void *)this)
> {
> this->PtrToMemberFunc;
> }


> Is there a reason why the static member function trick is so
> widely used? (Except when FuncThatWillCallback is part of a
> 3rd party lib).


As I said, I've never seen it used in correct code, period. In
pure C++, there are two widespread solutions: a callback
interface, using virtual functions, from which the client code
derives, and functional objects passed to a template. About the
only time I've seen anything else has been when interfacing to
legacy API's, in C, and in such cases, you can't pass a static
member function either, because the callback function must have
"C" linkage.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


 
Reply With Quote
 
paul
Guest
Posts: n/a
 
      04-29-2009
On Apr 28, 11:50*pm, "Alf P. Steinbach" <(E-Mail Removed)> wrote:
> * paul:
>
>
>
>
>
> > Hi,
> > I have seen the use of static member functions for callbacks by
> > passing the name of the static member function and the this pointer to
> > the code that will call back.
> > I was thinking the same can be achieved by using member function
> > pointers and the this pointer.

>
> > E.g.
> > void FuncThatWillCallback( MyClass::staticFunc, (void *)this)
> > {
> > * * staticFunc(this);
> > }
> > void FuncThatWillCallback( PtrToMemberFunc, (void *)this)
> > {
> > * * this->PtrToMemberFunc;
> > }

>
> Have you tried compiling any of the above?

No I didn't compile it and you're right the syntax is wrong. I just
wanted to get an opinion on the differences between the two.

> > Is there a reason why the static member function trick is so widely
> > used? (Except when FuncThatWillCallback is part of a 3rd party lib).

>
> Do you have any data that supports your contention that static member functions
> are widely used for callbacks in code that doesn't involve 3rd party libraries?

I have seen it a lot in the code I have worked on.

Paul
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      04-30-2009
* James Kanze:
>
> As I said, I've never seen it used in correct code, period. In
> pure C++, there are two widespread solutions: a callback
> interface, using virtual functions, from which the client code
> derives, and functional objects passed to a template. About the
> only time I've seen anything else has been when interfacing to
> legacy API's, in C, and in such cases, you can't pass a static
> member function either, because the callback function must have
> "C" linkage.


Well you can. Just not with standard's blessing. I think it would be difficult
to find a compiler that with the proper option (usually default) doesn't support it.


Cheers,

- Alf

--
Due to hosting requirements I need visits to <url: http://alfps.izfree.com/>.
No ads, and there is some C++ stuff! Just going there is good. Linking
to it is even better! Thanks in advance!
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      04-30-2009
On Apr 30, 4:07 am, "Alf P. Steinbach" <(E-Mail Removed)> wrote:
> * James Kanze:
> > As I said, I've never seen it used in correct code, period.
> > In pure C++, there are two widespread solutions: a callback
> > interface, using virtual functions, from which the client
> > code derives, and functional objects passed to a template.
> > About the only time I've seen anything else has been when
> > interfacing to legacy API's, in C, and in such cases, you
> > can't pass a static member function either, because the
> > callback function must have "C" linkage.


> Well you can. Just not with standard's blessing. I think it
> would be difficult to find a compiler that with the proper
> option (usually default) doesn't support it.


Sun CC generates a diagnostic, and as far as I know, you can't
turn the diagnostic off. And I've used C++ compilers where it
simply didn't work; the reason the standard doesn't allow it is
because there are cases where it won't work.

Note that Windows, or at least VC++, is a bit special here,
because the compiler doesn't use the standard mechanism for
linkage specification. Rather, it separates linkage into two
separate parts: name mangling (specified by ``extern "..."'')
and calling conventions (specified by Microsoft extensions, e.g.
__cdecl, __stdcall, __fastcall, etc.). These extensions do
apply to static member functions. Also, the compiler does
require that a pointer to a function and the function have the
same calling conventions. And the system functions, at least,
requre different calling conventions than the default, so you
have to use Microsoft's extensions if you're going to use a
function as a callback for Windows.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      04-30-2009
* James Kanze:
> On Apr 30, 4:07 am, "Alf P. Steinbach" <(E-Mail Removed)> wrote:
>> * James Kanze:
>>> As I said, I've never seen it used in correct code, period.
>>> In pure C++, there are two widespread solutions: a callback
>>> interface, using virtual functions, from which the client
>>> code derives, and functional objects passed to a template.
>>> About the only time I've seen anything else has been when
>>> interfacing to legacy API's, in C, and in such cases, you
>>> can't pass a static member function either, because the
>>> callback function must have "C" linkage.

>
>> Well you can. Just not with standard's blessing. I think it
>> would be difficult to find a compiler that with the proper
>> option (usually default) doesn't support it.

>
> Sun CC generates a diagnostic, and as far as I know, you can't
> turn the diagnostic off.


Have you tried

-erroff=badargtype2w

?

> And I've used C++ compilers where it
> simply didn't work;


Well I still think it would be difficult to find such a compiler!


> the reason the standard doesn't allow it is
> because there are cases where it won't work.


Yes. But aren't those are the cases where you (or someone else) have chosen for
C++ and C to have different calling conventions?



> Note that Windows, or at least VC++, is a bit special here,
> because the compiler doesn't use the standard mechanism for
> linkage specification. Rather, it separates linkage into two
> separate parts: name mangling (specified by ``extern "..."'')
> and calling conventions (specified by Microsoft extensions, e.g.
> __cdecl, __stdcall, __fastcall, etc.). These extensions do
> apply to static member functions. Also, the compiler does
> require that a pointer to a function and the function have the
> same calling conventions. And the system functions, at least,
> requre different calling conventions than the default, so you
> have to use Microsoft's extensions if you're going to use a
> function as a callback for Windows.


Yes. I've always thought that both the standard's approach and Microsoft's
approach are horrible, in this regard. It would be nice to be able to specify
such things via C++0x attribute notation.


Cheers,

- Alf

--
Due to hosting requirements I need visits to <url: http://alfps.izfree.com/>.
No ads, and there is some C++ stuff! Just going there is good. Linking
to it is even better! Thanks in advance!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Member function pointers to member functions with default arguments Hamish C++ 3 01-25-2008 06:46 AM
Can a static member function access non-static member? dolphin C++ 3 12-05-2007 12:39 PM
performance of static member function vs. instance member function 0to60 C++ 4 11-21-2003 05:25 PM
Pointers to non-static member functions, compiles with MSDEV, but not with GCC John Doe C++ 9 10-23-2003 10:01 PM
[Q] Pointers to Non-Static class member functions tekenenSPAM@BUSThotmailE.Rcom C++ 2 08-30-2003 03:19 AM



Advertisments