By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,510 Members | 1,815 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,510 IT Pros & Developers. It's quick & easy.

Address of a member function

P: n/a
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

Aug 10 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Neo wrote:
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) {}
};

--
Ian Collins.
Aug 10 '07 #2

P: n/a
Neo
On Aug 10, 11:22 am, Ian Collins <ian-n...@hotmail.comwrote:
Neo wrote:
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) {}

};

--
Ian Collins.
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

Aug 10 '07 #3

P: n/a
Neo wrote:
On Aug 10, 11:22 am, Ian Collins <ian-n...@hotmail.comwrote:
....
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.
Aug 10 '07 #4

P: n/a
Neo
On Aug 10, 1:25 pm, Gianni Mariani <gi3nos...@mariani.wswrote:
Neo wrote:
On Aug 10, 11:22 am, Ian Collins <ian-n...@hotmail.comwrote:
...
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.
thank you.

Aug 10 '07 #5

P: n/a
On Aug 10, 10:22 am, Neo <zingafri...@yahoo.comwrote:
On Aug 10, 11:22 am, Ian Collins <ian-n...@hotmail.comwrote:
Neo wrote:
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) {}
};
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.

--
James Kanze (GABI Software) email:james.ka...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Aug 11 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.