pointer to class member function

U

Uwe Grawert

Hello,

a C-function expects me to give a pointer to a function as parameter.
i want to give this function a pointer to a member function of a class
but the compiler gives me an error:

void (MyClass::*)(void*, void*) can not be converted to
void (*)(void*, void*)

i declared the function as protected. Is it not possible to give
a pointer to a member function of a class?
thnx!!
 
T

TB

Uwe Grawert skrev:
Hello,

a C-function expects me to give a pointer to a function as parameter.
i want to give this function a pointer to a member function of a class
but the compiler gives me an error:

void (MyClass::*)(void*, void*) can not be converted to
void (*)(void*, void*)

i declared the function as protected. Is it not possible to give
a pointer to a member function of a class?
thnx!!

A member function belongs to a class and can only be invoked with
a class object. You can't call an arbitrary non-static class member
function without an object at your will, nor pass it around like
a ordinary function pointer.
 
T

TB

TB skrev:
Uwe Grawert skrev:

A member function belongs to a class and can only be invoked with
a class object. You can't call an arbitrary non-static class member
function without an object at your will, nor pass it around
like a ordinary function pointer.
as if it was an ordinary function pointer

Consider this

class A {
public:
void * foo() { return this; }
};

void * bar( void * (*f)(void) ) {
return f();
}

What would bar() return in this call if you were
allowed to give it a member function pointer?

bar(&A::foo);
 
U

Uwe Grawert

Consider this
class A {
public:
void * foo() { return this; }
};

void * bar( void * (*f)(void) ) {
return f();
}

What would bar() return in this call if you were
allowed to give it a member function pointer?

bar(&A::foo);

I would say it would return a pointer to the object of class A, if that
is possible. but i´m calling bar(&A::foo) from inside of my A object.
in the sigc++ it is possible to give a pointer to a member function of a
class to a sigc_mem() function. if i have a instance of my class A why
shouldn´t it be possible to give a pointer to a member function?
 
T

TB

Uwe Grawert skrev:
I would say it would return a pointer to the object of class A, if that
is possible. but i´m calling bar(&A::foo) from inside of my A object.
in the sigc++ it is possible to give a pointer to a member function of a
class to a sigc_mem() function. if i have a instance of my class A why
shouldn´t it be possible to give a pointer to a member function?

You need an object to call the member function pointer on, e.g.:

A a;
void * (*func)();
func = &A::foo;
(a.*func)();

or

void A::baz( void * (A::*v) () ) {
(this->*v)();
}
 
U

Uwe Grawert

yo, thanks! i made the member function static now. doesn´t look to be
perfect but i´ll see :)
 
D

Default User

Uwe Grawert wrote:

Please don't top-post. See the netiquette section of the FAQs for more
details.
yo, thanks! i made the member function static now. doesn´t look to be
perfect but i´ll see :)

Understand that a static member function is very different from a
non-static. You won't be able to access any non-static data members or
non-static member functions. That's because you don't have an object to
call those on.

Unless there are some static data members you wish to access, you might
as well make this a stand-alone function.




Brian
 

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,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top