from c function func(double (*g(char*))) to cpp cool object design

from c function func(double (*g(char*))) to cpp cool object design - C++ - Programmation

Marsh Posté le 03-09-2008 à 09:43:29    

Hello,
I want to convert a c function  
 
void func(double (*g(char*))) ;
 
to a cpp object where we could specifie the function pointer per object instance.
I try functor ,member function pointer but the only paramater this function like to have is a static member function:(
Can someone help me deal with this problem?

Reply

Marsh Posté le 03-09-2008 à 09:43:29   

Reply

Marsh Posté le 03-09-2008 à 11:02:16    

Hello,  
 
   I think that boost::function can help you.

Reply

Marsh Posté le 03-09-2008 à 11:39:02    

a member function is different from a function.
For a given object A, the type of int A::f(int) is in fact :
 
int (A::*)(int);
or int (*)(A*,int);
 
use boost::function as stated.

Reply

Marsh Posté le 03-09-2008 à 11:42:19    

I would like not to add external dependencies to my project...There's no other solution:/?  
It seems to be a quite complex to solve the problem no?

Reply

Marsh Posté le 03-09-2008 à 12:10:50    

well, the dichotomy between function function member IS a hard problem. Boost solves it properly in allc ases of compiler/platform/type of function.
 
By dependencies, you mean at runtime ? If so, don't fear, boost is mostly headers only. If you want to minimize the boost packages needed for compilation , look at boost::BCP

Reply

Marsh Posté le 03-09-2008 à 13:39:40    

Ok, I've test a few things, but my c function don't want a boost::function as argument, it's incompatible. So can you explain moreover what you think?

Reply

Marsh Posté le 03-09-2008 à 15:41:37    

I was thinking the other way around.
You can't pass a C++ object to a C function anyway.
 
Best shot is making a class with a static method which encapsulates the actual method member into a boost::function.

Reply

Marsh Posté le 04-09-2008 à 11:26:45    

class CPP{
 
   static void CPPFunc(function1<double,char*> f){
           double (*fprim)(char*)=f;
           cfunc(fprim);
}
};
?????????
no I dont understand...:((
 

Reply

Marsh Posté le 04-09-2008 à 14:32:15    

or  
 
class CPP{
private:
function2<double,CPP*,char*>* g;
public:
static double gcall(char*c){
return g(c);
}
virtual double gimpl(char*){blabla}
void exec(){
     g=new function2<double,CPP*,char*>(bind(CPP::gimpl,this,_1));
     cfunc(gcall);
}
Is it more like you're thinking?
No there's always a problem...arg


Message édité par mp3butcher le 04-09-2008 à 14:41:36
Reply

Marsh Posté le 04-09-2008 à 19:58:26    

WTF

Reply

Marsh Posté le 04-09-2008 à 19:58:26   

Reply

Marsh Posté le 05-09-2008 à 10:10:00    

Excuse me I don't know what to do with function<> for my problem...:(

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed