Conversion problem

P

PSN

Hi all,
I have the following code, which ends up with an error, cannot convert
from "void (A::*)(unsigned int)" to "void (*)(unsigned int)". Could
anyone please explain why ?? It works well, when the function is
defined static. However I dont understand why does it have to be
static.


class A
{
public:
void print(unsigned int i) { cout << i << endl; }
};

int main()
{
A objA;
typedef void (*PRINT_FUNC) (unsigned int);
objA.print(437);
PRINT_FUNC printFunc;
printFunc = &A::print;
printFunc(438);

return 0;
}


Thanks again,
P.
 
I

Ian Collins

Hi all,
I have the following code, which ends up with an error, cannot convert
from "void (A::*)(unsigned int)" to "void (*)(unsigned int)". Could
anyone please explain why ?? It works well, when the function is
defined static. However I dont understand why does it have to be
static.

You are trying to convert a class member function pointer to a free
function pointer. The two are not the same.
class A
{
public:
void print(unsigned int i) { cout<< i<< endl; }
};

int main()
{
A objA;
typedef void (*PRINT_FUNC) (unsigned int);
objA.print(437);
PRINT_FUNC printFunc;
printFunc =&A::print;

In C terms, A::print can be represented as

typedef void (*PRINT_FUNC) (A*, unsigned int);
 
P

Paul Bibbings

PSN said:
Hi all,
I have the following code, which ends up with an error, cannot convert
from "void (A::*)(unsigned int)" to "void (*)(unsigned int)". Could
anyone please explain why ?? It works well, when the function is
defined static. However I dont understand why does it have to be
static.


class A
{
public:
void print(unsigned int i) { cout << i << endl; }
};

int main()
{
A objA;
typedef void (*PRINT_FUNC) (unsigned int); // #1
objA.print(437);
PRINT_FUNC printFunc;
printFunc = &A::print; // #2
printFunc(438); // #3

return 0;
}


Thanks again,
P.

A::print(unsigned) is a member function. You recognise this in line #2,
above. So your typedef in line #1 needs to recognise this too, as:

typedef void (A::*PRINT_FUNC) (unsigned int);

as does your call in line #3:

(objA.*printFunc)(438);

AFAIK, some implementations will accept a pointer to static member
function as an ordinary function pointer. However (I think) I believe
that this is incorrect - an extension.

Regards

Paul Bibbings
 
A

Andrew Poelstra

[something]

Hey guys,

I'm using slrn and my cat walked all over my keyboard, and now
there's a star next to Ian's message and I can't mark it as read.

Do you know what this is and how to undo it?

Thanks
 
A

Andrew Poelstra

[something]

Hey guys,

I'm using slrn and my cat walked all over my keyboard, and now
there's a star next to Ian's message and I can't mark it as read.

Do you know what this is and how to undo it?

Thanks

I got it. Well, I don't know what it was, but I closed and re-opened
comp.lang.c++ and the star went away.

Sorry to spam up the group.
 
R

Rolf Magnus

Stuart said:
No.

See FAQ 33.2, by the looks of it. Specifically:

'Note: static member functions do not require an actual object to be
invoked, so pointers-to-static-member-functions are usually
type-compatible with regular pointers-to-functions. However, although it
probably works on most compilers, it actually would have to be an extern
"C" non-member function to be correct, since "C linkage" doesn't only
cover things like name mangling, but also calling conventions, which
might be different between C and C++.'

That's actually not about function pointers vs. static member functions, but
about C vs. C++ linkage. A static member function can be used with a regular
pointer to function in C++ (there is no such thing as a "pointer to static
member").
But if you have a pure C API that wants a pointer to a function, then you
need to pass the address of a function with C linkage, and static member
functions can't have C linkage.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,147
Messages
2,570,833
Members
47,377
Latest member
MableYocum

Latest Threads

Top