Re: Assigning a member function to signal callback function pointer
On Dec 25, 9:11 am, Ian Collins <ian-n...@hotmail.com> wrote:
On Dec 24, 11:24 pm, Ian Collins <ian-n...@hotmail.com> wrote:
Recall that an ordinary class member function has access
to member variables of an individual class instance. You
may think that by passing a pointer to a member function
from another member function, as a callback function, when
the callback function gets invoked it will be invoked for
the same class instance, but this is not the case. The
timer_create API doesn't know about any objects, it only
takes an ordinary function pointer. With C++, a function
pointer is not enough, you need a class instance to go
with it. Unless, of course, it's a static class member
function that's not associated with any class instance.
Which still isn't a valid function to pass to a C
interface, the linkage is wrong.
You may mean "calling convention", not "linkage", although
in all compilers that I know of that let you specify the
calling convention, it's part of the type (e.g. __stdcall
vs. __cdecl with MSVC).
The standard specifies a means of specifying the calling
convention; it's called a linkage specification.
No, I should have said "Linkage specification". Nothing to do
with calling conventions.
Linkage specification is the C++ syntax for specifying the
calling conventions: "C++", "C", "Fortran", etc. ("C++" and "C"
are required; any others are implementation defined.)
A static member function does not have extern "C" linkage.
More precisely, "A C language linkage is ignored for the names
of class members and the member function type of class member
Passing C++ function pointers to functions with C linkage is
not an issue, the linkage is used by the linker, but passing
function pointers around is not the same as linking object
files together. Linkage doesn't come into play there.
Passing a pointer to a C++ function to a function expecting a
pointer to a C function is an issue, although many compilers
will ignore the difference. Others aren't so lax.
The linkage is part of the type. Using the address of a
function with C++ linkage to initialize a pointer to a function
with C linkage is a diagnosable error; a C++ compiler is
required to emit a diagnostic. Some compilers have bugs,
however, and don't. (And why Microsoft decided to invent a
completely different mechanism, when the standard already
defined one, I don't know. Logically, instead of new keywords,
I would have expected ``extern "stdcall"'', etc. Not that it
makes much difference in practice.)
James Kanze (GABI Software) email:email@example.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34