function pointers in C++
Hello!
Suppose I have:
class Foo
{
public:
Foo(); ~Foo(); int bar(int); };
Now, why I can't do this:
Foo *f = new Foo();taz(f->bar);
Where taz()'s protype is:
void taz(int (*f)(int));
In plain C, I can do:
int f(int) { ... }
taz(f);
without a problem. Is there a way to accomplish such a thing (i.e. to pass the pointer of a C++ class's member function to another function that expects a pointer to function as an argument)?
Regards,
On Mon, 2004-03-15 11:28:19 +0200, [email protected] <[email protected]> wrote in message <[email protected]>:
Quoted 12 lines
Hello!
Suppose I have:
class Foo
{
public:Foo(); ~Foo(); int bar(int); };
Quoted 8 lines
Now, why I can't do this:
Foo *f = new
Foo();taz(f->bar);Where taz()'s protype is:
void
taz(int (*f)(int));
You're looking for "static"ally declared member functions.
MfG, JBG
Signature · 4 lines
Jan-Benedict Glaw [email protected] . +49-172-7608481 "Eine Freie Meinung in einem Freien Kopf | Gegen Zensur | Gegen Krieg fuer einen Freien Staat voll Freier Bürger" | im Internet! | im Irak! ret = do_actions((curr | FREE_SPEECH) & ~(NEW_COPYRIGHT_LAW | DRM | TCPA));
Jan-Benedict Glaw <[email protected]>:
Quoted 4 lines
On Mon, 2004-03-15 11:28:19 +0200, [email protected] <[email protected]>
Quoted 1 line
void
taz(int (*f)(int));You're looking for "static"ally declared member functions.
And in that case I can't use 'this' inside the function. Brilliant... :-(
Thanx for the reply.
Regards,
Probably what you need is the "pointer to member". It's described in chapter 15, section 5 in "The C++ Programming Language", from Stroustrup.
Quoted 32 lines · [email protected] wrote:
Hello!
Suppose I have:
class Foo { public:
Foo(); ~Foo(); int bar(int); };Now, why I can't do this:
Foo *f = new
Foo();taz(f->bar);Where taz()'s protype is:
void
taz(int (*f)(int));In plain C, I can do:
int f(int) { ... } taz(f);
without a problem. Is there a way to accomplish such a thing (i.e. to pass the pointer of a C++ class's member function to another function that expects a pointer to function as an argument)?
Regards,
Signature · 4 lines
- To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Some time ago I made a small library to tackle this problem, it's in
http://sourceforge.net/projects/cppcback/
Also there is an implementation of a similar mechanism in the libraries found in http://www.boost.org.
Quoted 13 lines · Darío Mariani
Probably what you need is the "pointer to member". It's described in chapter 15, section 5 in "The C++ Programming Language", from Stroustrup.
Quoted 6 lines · [email protected] wrote:
Hello! Suppose I have: class Foo { public:
Foo(); ~Foo(); int bar(int); }; Now, why I can't do this: Foo *f = newFoo();taz(f->bar); Where taz()'s protype is: voidtaz(int (*f)(int)); In plain C, I can do: int f(int) { ... } taz(f); without a problem. Is there a way to accomplish such a thing (i.e. to pass the pointer of a C++ class's member function to another function that expects a pointer to function as an argument)? Regards, --
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 5 lines
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
hi.
well check out mem_fun() which is standard library function...this takes the pointer to a member function as its argument and produces something that can be called for a pointer to the member's class.
Ex for your code...
taz(mem_function(&Foo::bar) should work...it should take the Foo* argument and return what Foo::bar() returns !!
regards,
Ashu
Quoted 37 lines · On Mon, 2004-03-15 at 14:58, [email protected] wrote:
Hello!
Suppose I have:
class Foo
{
public:Foo(); ~Foo(); int bar(int); };Now, why I can't do this:
Foo *f = new
Foo();taz(f->bar);Where taz()'s protype is:
void
taz(int (*f)(int));In plain C, I can do:
int f(int) { ... }
taz(f);without a problem. Is there a way to accomplish such a thing (i.e. to pass the pointer of a C++ class's member function to another function that expects a pointer to function as an argument)?
Regards,
Signature · 4 lines
- To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html