Address of a member function

N

Neo

hi,
I searched in the groups here but didnt find any post answering my
question. My question is I need the address of the member function in
the same class. Is there anyway to go about it other than making the
func static?
For ex:

class foo {
some_type x
void fa();
----
}

Now in its constructor-
foo::foo() {
x = <adddress_of_fa>;
}

how do I do the above?? I tried &(this->fa) but it says "C++ forbids
taking the address of a bound member function to form a pointer"


Thanks,
Neo
 
I

Ian Collins

Neo said:
hi,
I searched in the groups here but didnt find any post answering my
question. My question is I need the address of the member function in
the same class. Is there anyway to go about it other than making the
func static?
For ex:

class foo {
some_type x
void fa();
----
}

Now in its constructor-
foo::foo() {
x = <adddress_of_fa>;
}

how do I do the above?? I tried &(this->fa) but it says "C++ forbids
taking the address of a bound member function to form a pointer"

class foo {

typedef void (foo::*some_type)();

some_type x;

void fa();

foo() : x(&foo::fa) {}
};
 
N

Neo

class foo {

typedef void (foo::*some_type)();

some_type x;

void fa();

foo() : x(&foo::fa) {}

};

I have a correction-
x needs something of type void(*)() but can I pass the address
void(foo::*)() to it Or should it be a static function only?

thanks
 
G

Gianni Mariani

Neo said:
I have a correction-
x needs something of type void(*)() but can I pass the address
void(foo::*)() to it Or should it be a static function only?

It must be a static function only.
 
J

James Kanze

I have a correction-
x needs something of type void(*)() but can I pass the address
void(foo::*)() to it Or should it be a static function only?

The only thing you can use for type void(*)() is a free function
or a static function, with the correct signature.

In C++, the usual solution here would be to replace the
void(*)() with something like:

struct Callback
{
virtual ~Callback() {}
virtual void operator()() const = 0 ;
} ;

and use a Callback const*. If you have access to the sources
where the callback is used, do this. Otherwise, you're sort of
stuck.

Note too that if you pass the pointer to function to a C
function, the function itself has to be `extern "C"'. Which
means that even a static member function can't be used.
 

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

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top