I've an old C function that calls a function pointed by a global function pointer. How do I get it to call a member function of a class object determined at run-time. It'll be complied in a C++ project of course.
My main motive is reusability here. So I don't want to change the C function.

Its something like this:

void (*foo)(int);

   class coo
   {
   public:
           void func(int x){ ; }
   };

   void cfunc()
   {
        foo(10); 
   }

Recommended Answers

All 4 Replies

you can write a wrapper function in the *.cpp file that the C function calls, then let the wrapper function call the class method. The following function can be called by C programs.

// c++ file
extern "C" int foo()
{
    // call c++ class function
}

Thanks,
I wrote a new .cpp file for these wrapper functions, but its not compiling. I'm getting a long list of errors.
CanvasObj class is defined as a template. Could that be a problem?

#include "CanvasObj.h"

/* CanvasObj member function pointers */
typedef
void (CanvasObj::*)(int, int, unsigned long)  putptr;

typedef
unsigned long (CanvasObj::*)(int, int)        getptr;

putptr fputpix;
getptr fgetpix;

extern "C"
void putpixel(int x, int y, unsigned long c)
{
  fputpix(x, y, c);
}

extern "C"
unsigned long getpixel(int x, int y)
{
  fgetpix(x, y);
}


/* called by CanvasObj class member as
    setpix(&(this->putpixel), &(this->getpixel))
*/
void setpix(putptr fptr1, getptr fptr2)
{
  fputpix = fptr1;
  fgetpix = fptr2;
}

instance methods take an extra argument, the pointer to the instance that they are invoked on (the "this" pointer). so i have no idea how you think you are going to try to call an instance method without an instance

> CanvasObj class is defined as a template. Could that be a problem?
yes, it is a problem. but it is solved easily enough.
template typedefs are not legal in c++98. (they would be in c++0x)
as of now, you can not write

template< typename T > class CanvasObj ;

template< typename T >
typedef void ( CanvasObj<T>::*putptr_t )( int, int, unsigned long )  ;

the usual work around for this is to declare typedef inside a template class.

template< typename T > class CanvasObj ;

template< typename T > struct types
{
  typedef void ( CanvasObj<T>::*putptr_t )( int, int, unsigned long )  ;
  static putptr_t fputpix ;
};

template< typename T > typename types<T>::putptr_t types<T>::fputpix ;

template< typename T >
void setpix( typename types<T>::putptr_t fptr1 )
{
  types<T>::fputpix = fptr1;
}

/*
  called by CanvasObj class member as
    setpix( &(this->putpixel) )
*/

> My main motive is reusability here. So I don't want to change the C function.
the really serious problem is the one that bugmenot pointed out.
if the CanvasObj<T> object is a singleton, the problem is easily solved.
if not, it would require some complex (and ugly) code. it would be much easier and also much better to abandon the attempt at reusability of the C code. and rewrite the part involving the callback from scratch.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.