public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Getting pointers to member functions with g++
@ 2004-01-02 18:12 Bernard WEISSER
  0 siblings, 0 replies; only message in thread
From: Bernard WEISSER @ 2004-01-02 18:12 UTC (permalink / raw)
  To: gcc-help

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) ;

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2004-01-02 18:12 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-01-02 18:12 Getting pointers to member functions with g++ Bernard WEISSER

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).