This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Getting pointers to member functions with g++
- From: Bernard WEISSER <bernard dot weisser at wanadoo dot fr>
- To: gcc-help at gcc dot gnu dot org
- Date: Fri, 02 Jan 2004 19:13:15 +0100
- Subject: Getting pointers to member functions with g++
Hi,
In order to use C++ classes from the Ada GNAT environement (and after
trying unsuccessfully to use the GNAT "CPP_Class" and "CPP_Constructor"
pragmas), I should want to call the class member functions
(constructors, destructor, others functions) directly by creating C or
C++ functions having inside their arguments list the address of the
class object to use. For example, for the following "my_class" class, I
would like to write the 4 following interacing functions :
void if_constructor (my_class *ptr) ;
void if_destructor (my_class *ptr) ;
int if_foo (my_class *ptr) ;
int if_foo (my_class *ptr, int i) ;
To do so, I have to know the addresses of the corresponding member
functions. I read in the GCC info file node "Bound member functions"
that it is possible to get pointers to the class member functions using
the indicated method indicated there.
Practically, I cannot use this method (see the following example
"my_class") on the constructor and on the desctructor, and I cannot also
discriminate between the 2 "foo" variants.
When compiling my example with the command "g++ -c
-Wno-pmf-conversions", I get the following messages :
:53: syntax error before `)' token
:54: taking address of destructor
:55: no matches converting function `foo' to type `int (*)(class
my_class*)'
:46: candidates are: int my_class::foo()
:47: int my_class::foo(int)
:56: syntax error before `)' token
How would you modify my code in order to make it compile properly ?
Thank you for you help,
Bernard.
class my_class
{
public:
my_class () ;
~my_class ();
int foo () ;
int foo (int i) ;
} ;
typedef int (*fptr1)(my_class *) ;
typedef int (*fptr2)(my_class *, int) ;
fptr1 p1 = (fptr1)(&my_class::my_class) ;
fptr1 p2 = (fptr1)(&my_class::~my_class) ;
fptr1 p3 = (fptr1)(&my_class::foo) ;
fptr2 p4 = (fptr2)(&my_class::foo) ;