Assign C ++ instance method to global function pointer?

Hi,

My project structure is as follows:

\- base  (C static library)
     callbacks.h
     callbacks.c
     paint_node.c
     . 
     .
     * libBase.a

\-app (C++ application)
     main.cpp

In the 'base' C library, I declared global-function-pointer as:

in singleheader file

callbacks.h

#ifndef CALLBACKS_H_
#define CALLBACKS_H_

extern void (*putPixelCallBack)();
extern void (*putImageCallBack)();

#endif /* CALLBACKS_H_ */

in one C file they are initialized as

callbacks.c

#include "callbacks.h"
void (*putPixelCallBack)();
void (*putImageCallBack)();

Other C files access these callbacks as:

paint_node.c

#include "callbacks.h"
void paint_node(node *node,int index){

  //Call callbackfunction
  .
  .

  putPixelCallBack(node->x,node->y,index);
}

I will compile these C files and create the libBase.a static library

Then in a C ++ application

I want to assign a C ++ instance method to this global pointer function:

I did something like the following:

in the Sacm.cpp file

#include "Sacm.h"

extern void (*putPixelCallBack)();
extern void (*putImageCallBack)();

void Sacm::doDetection()
{
  putPixelCallBack=(void(*)())&paintPixel;
  //call somefunctions in 'libBase' C library

}

void Sacm::paintPixel(int x,int y,int index)
{
 qpainter.begin(this);
 qpainter.drawPoint(x,y);
 qpainter.end();
}

But when compiling it gives an error:

sacmtest.cpp: - 'void SACM:: doDetection(): sacmtest.cpp: 113: : ISO ++ - -. '& Sacm:: paintPixel sacmtest.cpp: 113: : 'void (Sacm::) (int, int, int) 'void ()()

?

+3
3

++, [1]. , . , , :

 Sacm* sacm_global;

 void sacm_global_paintPixel(int x,int y,int index)
 {
   sacm_global->paintPixel(x, y, index);
 }

void Sacm::doDetection()
{
  putPixelCallBack = &sacm_global_paintPixel;
  //call somefunctions in 'libBase' C library
}

- .

+7

. -, , , , :

Sacm *callbackSacm;

extern "C"  // since it sounds like it called from a C library
void call_paintPixel(int x, int y, int index) {
   callbackSacm->paintPixel(x, y, index);
}

void Sacm::doDetection() {
   callbackSacm = this;
   putPixelCallBack = call_paintPixel;
}
+2

static. - , this - , , . , :

  • private protected .
  • private protected, .
  • , .
+2

Source: https://habr.com/ru/post/1742380/


All Articles