473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 4351
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.