C++ 从函数指针调用具有多个参数的多个函数?
我正在尝试定义一个函数指针,用于调用具有不同参数的多个函数,但现在我的想法被卡住了。有没有人有想法,或者可以看到我做错了什么,因为我没有:微笑:如果你能帮助我,这将非常有帮助C++ 从函数指针调用具有多个参数的多个函数?,c++,C++,我正在尝试定义一个函数指针,用于调用具有不同参数的多个函数,但现在我的想法被卡住了。有没有人有想法,或者可以看到我做错了什么,因为我没有:微笑:如果你能帮助我,这将非常有帮助 //Goal: Calling the sum function from the function pointer with the t1 struct as parameter //Theory: As my theory after first param the function will go -4 dow
//Goal: Calling the sum function from the function pointer with the t1 struct as parameter
//Theory: As my theory after first param the function will go -4 down in memory and look for the second variable but nope
float sum(float &x, float &y) //a random test-foo function
{
float s = x + y;
printf_s("(%X)x: %f + (%X)y: %f | Result: %f\n",&x, x, &y, y, s);
return s;
}
typedef float(*fsum)(void* params);
fsum fsm = (fsum)∑
struct t1 {
float f[2]; //the params will be here
}tx1;
int main()
{
tx1.f[0] = 4.3; tx1.f[1] = 2; //setting values on the params
printf_s("tx1: 0x%X\ntx1.f[0]: 0x%X\ntx1.f[1]: 0x%X\n", &tx1, &tx1.f[0], &tx1.f[1]);
fsm(&tx1.f[0]); //calling the function pointer
getchar();
return 0;
}
我的主要目标是稍后使用它来调用具有不同参数的不同函数,只需一个函数指针和一个指向参数的指针
比如:
第二个问题:假设我有一个.dll,我用C++编写了一个函数,叫做“FUU”。我在另一个进程中加载了DLL,如何加载第二个不同的C++ DLL?“/u”> P> > P>我不喜欢你的模式,但是如果你真的坚持它,你可以使用多态性。
class BaseProcessor{
public:
virtual float func(void *); };
class Processor1 : public BaseProcessor {
public:
type3 func(void * param){
type3 func1((type1*) param);
}
private:
type3 func1(type1 * param){
//implementation goes here
} };
class Processor2 : public BaseProcessor {
public:
type3 func(void * param){
return func2((type2 *) param);
}
private:
type3 func2(type2 * param){
//implementation goes here
}
};
像这样使用它
BaseProcessor * processor;
if(statement1)
processor = new Processor1;
else if(statement1)
processor = new Processor2;
processor->func(paramPointer);
<代码> Vult*/COD>是一个坏的C++设计的警告。
如果没有不明确的签名,您可以直接重载func()
,或者将实现附加到数据:
class base_t{
public:
virtual float sum();
}
class t1 : public base_t {
public:
float f[2]; //the params will be here
sum(){/* Implemenation goes here*/}
};
每个帖子都有一个问题,请决定这是C++还是C,并相应设置标签。如果您有第二个问题,请在单独的问题中发布该问题,不要尝试在单个帖子中合并多个问题,特别是当它们看起来不同的主题时,这样会有不在同一专业领域的答案。为什么要采取这种方法?我之所以问,是因为C++的设计是不这样做的,而你是在和语言作战。就好像你想用C++编写C代码一样。而且很难阅读和理解C代码。
class base_t{
public:
virtual float sum();
}
class t1 : public base_t {
public:
float f[2]; //the params will be here
sum(){/* Implemenation goes here*/}
};