* bound pointer to member
@ 2011-02-23 10:28 Klaus Rudolph
2011-02-23 10:29 ` Jonathan Wakely
0 siblings, 1 reply; 5+ messages in thread
From: Klaus Rudolph @ 2011-02-23 10:28 UTC (permalink / raw)
To: gcc-help
Hi,
I search for a simple code snipped which shows how bound pointers work.
I always end up in the message :
main.cpp:51: error: ISO C++ forbids taking the address of a bound member function to form a pointer to member function. Say â&A::Func2â
Can anyone post a code snippet which compiles on g++? Is there a need to give any special options to enable this language extension?
Thanks
Klaus
--
Empfehlen Sie GMX DSL Ihren Freunden und Bekannten und wir
belohnen Sie mit bis zu 50,- Euro! https://freundschaftswerbung.gmx.de
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: bound pointer to member
2011-02-23 10:28 bound pointer to member Klaus Rudolph
@ 2011-02-23 10:29 ` Jonathan Wakely
2011-02-23 12:09 ` Klaus Rudolph
0 siblings, 1 reply; 5+ messages in thread
From: Jonathan Wakely @ 2011-02-23 10:29 UTC (permalink / raw)
To: Klaus Rudolph; +Cc: gcc-help
On 23 February 2011 10:19, Klaus Rudolph wrote:
> Hi,
>
> I search for a simple code snipped which shows how bound pointers work.
> I always end up in the message :
>
> main.cpp:51: error: ISO C++ forbids taking the address of a bound member function to form a pointer to member function. Say ‘&A::Func2’
>
> Can anyone post a code snippet which compiles on g++?
$ cat bound.cc
struct Sq {
int f(int i) { return i*i; }
};
int main()
{
typedef int (*funcptr_type)(Sq*, int);
funcptr_type f = (funcptr_type)(&Sq::f);
Sq sq;
return f(&sq, 4);
}
$ g++ bound.cc -Wno-pmf-conversions
$ ./a.out ; echo $?
16
$
>Is there a need to give any special options to enable this language extension?
As stated in the manual, use -Wno-pmf-conversions to suppress a
warning, but it still works even if the warning is issued.
http://gcc.gnu.org/onlinedocs/gcc/Bound-member-functions.html
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: bound pointer to member
2011-02-23 10:29 ` Jonathan Wakely
@ 2011-02-23 12:09 ` Klaus Rudolph
2011-02-23 12:22 ` Jonathan Wakely
2011-02-23 12:40 ` David Hagood
0 siblings, 2 replies; 5+ messages in thread
From: Klaus Rudolph @ 2011-02-23 12:09 UTC (permalink / raw)
To: Jonathan Wakely; +Cc: gcc-help
> > Hi,
> >
> > I search for a simple code snipped which shows how bound pointers work.
> > I always end up in the message :
> >
> > main.cpp:51: error: ISO C++ forbids taking the address of a bound member
> function to form a pointer to member function. Â Say â&A::Func2â
> >
> > Can anyone post a code snippet which compiles on g++?
>
> $ cat bound.cc
> struct Sq {
> int f(int i) { return i*i; }
> };
>
> int main()
> {
> typedef int (*funcptr_type)(Sq*, int);
>
> funcptr_type f = (funcptr_type)(&Sq::f);
>
> Sq sq;
>
> return f(&sq, 4);
> }
OK, maybe a missunderstanding or a wrong naming for my problem...
What I am searching for is the following:
class A
{
void AnyFunc();
}
int main()
{
A a1;
A a2;
...anyPointerDefinition ptr1;
...anyPointerDefinition ptr2;
ptr1= &(a1.Func);
ptr2= &(a2.Func);
//call the pointers as functions
ptr1();
ptr2();
}
where ptr1 and ptr2 must contain the ptr to the method and the ptr to
the object. This extension is available on borland as I know?!
Any idea?
Thanks
Klaus
--
Empfehlen Sie GMX DSL Ihren Freunden und Bekannten und wir
belohnen Sie mit bis zu 50,- Euro! https://freundschaftswerbung.gmx.de
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: bound pointer to member
2011-02-23 12:09 ` Klaus Rudolph
@ 2011-02-23 12:22 ` Jonathan Wakely
2011-02-23 12:40 ` David Hagood
1 sibling, 0 replies; 5+ messages in thread
From: Jonathan Wakely @ 2011-02-23 12:22 UTC (permalink / raw)
To: Klaus Rudolph; +Cc: gcc-help
On 23 February 2011 10:54, Klaus Rudolph wrote:
>
> What I am searching for is the following:
>
> class A
> {
> void AnyFunc();
> }
>
> int main()
> {
> A a1;
> A a2;
>
> ...anyPointerDefinition ptr1;
> ...anyPointerDefinition ptr2;
>
> ptr1= &(a1.Func);
> ptr2= &(a2.Func);
>
> //call the pointers as functions
> ptr1();
> ptr2();
> }
>
> where ptr1 and ptr2 must contain the ptr to the method and the ptr to
> the object. This extension is available on borland as I know?!
>
> Any idea?
I don't think G++ supports that.
You can do it using the TR1 library extensions:
#include <tr1/functional>
struct A
{
int f();
};
int main()
{
using std::tr1::function;
using std::tr1::bind;
A a;
function<int()> f = bind(&A::f, a);
return f();
}
The same functionality is available in the Boost library too.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: bound pointer to member
2011-02-23 12:09 ` Klaus Rudolph
2011-02-23 12:22 ` Jonathan Wakely
@ 2011-02-23 12:40 ` David Hagood
1 sibling, 0 replies; 5+ messages in thread
From: David Hagood @ 2011-02-23 12:40 UTC (permalink / raw)
To: Klaus Rudolph; +Cc: Jonathan Wakely, gcc-help
On Wed, 2011-02-23 at 11:54 +0100, Klaus Rudolph wrote:
> OK, maybe a missunderstanding or a wrong naming for my problem...
>
> What I am searching for is the following:
>
> class A
> {
> void AnyFunc();
> }
>
> int main()
> {
> A a1;
> A a2;
>
> ...anyPointerDefinition ptr1;
> ...anyPointerDefinition ptr2;
>
> ptr1= &(a1.Func);
> ptr2= &(a2.Func);
>
> //call the pointers as functions
> ptr1();
> ptr2();
> }
You probably want to use the Boost Function library for this:
http://www.boost.org/doc/libs/1_46_0/doc/html/function/tutorial.html#id1275012
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2011-02-23 12:22 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-02-23 10:28 bound pointer to member Klaus Rudolph
2011-02-23 10:29 ` Jonathan Wakely
2011-02-23 12:09 ` Klaus Rudolph
2011-02-23 12:22 ` Jonathan Wakely
2011-02-23 12:40 ` David Hagood
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).