468,315 Members | 1,447 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,315 developers. It's quick & easy.

pointer to class member function

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!!
Mar 4 '06 #1
7 3986
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.

--
TB @ SWEDEN
Mar 4 '06 #2
TB
TB skrev:
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.

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);

--
TB @ SWEDEN
Mar 4 '06 #3
> 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?
Mar 4 '06 #4
Uwe Grawert wrote:
Hello, i declared the function as protected. Is it not possible to give
a pointer to a member function of a class?
thnx!!


Maybe mem_fn can help ya?
Mar 4 '06 #5
TB
Uwe Grawert skrev:
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?


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)();
}

--
TB @ SWEDEN
Mar 4 '06 #6
yo, thanks! i made the member function static now. doesn´t look to be
perfect but i´ll see :)

TB wrote:
Uwe Grawert skrev:
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?

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)();
}

Mar 4 '06 #7
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
--
If televison's a babysitter, the Internet is a drunk librarian who
won't shut up.
-- Dorothy Gambrell (http://catandgirl.com)
Mar 4 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

37 posts views Thread by Ben | last post: by
15 posts views Thread by Albert | last post: by
7 posts views Thread by jon wayne | last post: by
6 posts views Thread by keepyourstupidspam | last post: by
12 posts views Thread by WaterWalk | last post: by
7 posts views Thread by WaterWalk | last post: by
5 posts views Thread by Tim Frink | last post: by
5 posts views Thread by Immortal Nephi | last post: by
7 posts views Thread by ghulands | last post: by
reply views Thread by Teichintx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.