472,958 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

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 4322
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1,...
37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
15
by: Albert | last post by:
Hi, I need to pass a pointer-to-member-function as a parameter to a function which takes pointer-to-function as an argument. Is there any way to do it besides overloading the function? Here...
7
by: jon wayne | last post by:
Hi I'm a little confused here about the lifetime of a static pointer to member function, Say, I declare,define & initialize a static ptr to mem function in the header file of a class(the class...
6
by: keepyourstupidspam | last post by:
Hi, I want to pass a function pointer that is a class member. This is the fn I want to pass the function pointer into: int Scheduler::Add(const unsigned long timeout, void* pFunction, void*...
12
by: WaterWalk | last post by:
Hello. I am rather confused by the type of a pointer to class data member. Many c++ texts say that pointer to data member has a special syntax. For example the following class: class MyClass {...
7
by: WaterWalk | last post by:
Hello. I thought I understood member function pointers, but in fact I don't. Consider the following example: class Base { public: virtual ~Base() {} }; class Derived : public Base {
5
by: Tim Frink | last post by:
Hi, I'm experimenting with function pointers and found two questions. Let's assume this code: 1 #include <iostream> 2 class A; 3 4 //////////////////////////////////////////// 5 class B
5
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call...
7
by: ghulands | last post by:
I am having trouble implementing some function pointer stuff in c++ An object can register itself for many events void addEventListener(CFObject *target, CFEventHandler callback, uint8_t...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.