功能指针 [英] Function Pointers

查看:67
本文介绍了功能指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我试图理解功能指针的使用。我写了一个

小以了解其用途,但我不相信它是

正确的实现。


你能展示吗?在这种情况下,我是一种删除switch语句的方法吗?

谢谢,

Kapil


#include" stdafx.h" ;

使用命名空间std;


float Plus(浮动a,浮动b){return a + b; } $ / $
float减号(浮动a,浮动b){返回a - b; } $ / $
float Mult(float a,float b){return a * b; } $ / $
浮动Div(浮动a,浮动b){返回a / b; } $ / $
float Func(float,float);


void Switch_With_Function_Pointer(float,float,float

(* pt2Func)( float,float));

int main()

{


浮动a,b;

float(* Func)(float,float);

char opCode;

cout<< 输入数字和操作 << endl;

cin>> a>> b>> opCode;

//我真的需要这个开关,有更快的方法!!

开关(opCode)

{

case''+'':Func = Plus;

break;

case'' - '':Func =减;

休息;

case''*'':Func = Mult;

休息;

case''/'':Func = Div;

休息;

}


Switch_With_Function_Pointer(a,b,Func);

返回0;

}


void Switch_With_Function_Pointer(浮动a,浮动b,浮动

(* pt2Func)(浮动a ,浮动b))

{

浮动结果= pt2Func(a,b);

cout<< 切换功能结果是 <<结果<< endl;

}

Hi,
I am trying to understand the use of Function Pointers. I wrote a
small to understand the use but am not convinced that it is the
correct implementation.

Can you show me a way to remove the switch statement in this case ?
Thanks,
Kapil

#include "stdafx.h"
using namespace std;

float Plus(float a, float b) {return a + b; }
float Minus(float a, float b) {return a - b; }
float Mult(float a, float b) {return a * b; }
float Div(float a, float b) { return a / b; }
float Func(float,float);

void Switch_With_Function_Pointer(float,float,float
(*pt2Func)(float,float));
int main()
{

float a,b;
float (*Func)(float,float);
char opCode;
cout << "Enter the numbers and operation" << endl;
cin >> a >> b >> opCode;
// Do I really need this switch ,is there a faster way !!
switch(opCode)
{
case ''+'' : Func = Plus;
break;
case ''-'' : Func = Minus;
break;
case ''*'' : Func = Mult;
break;
case ''/'' : Func = Div;
break;
}

Switch_With_Function_Pointer(a,b,Func);
return 0;
}

void Switch_With_Function_Pointer(float a,float b,float
(*pt2Func)(float a,float b))
{
float result = pt2Func(a,b);
cout << "Switch with function result is " << result << endl;
}

推荐答案

" Kapil Khosla" < KH ********* @ yahoo.com>写道...
"Kapil Khosla" <kh*********@yahoo.com> wrote...
我试图理解功能指针的使用。我写了一个小的了解用法,但我不相信它是正确的实现。


你的实现没问题。

你能告诉我一种在这种情况下删除switch语句的方法吗?


将函数指针放入映射< char,double(*)(double,double)>

并使用opCode提取所需的指针关键。

谢谢,
Kapil

#include" stdafx.h"
使用命名空间std;

float Plus(float a,float b){return a + b;浮动减(浮动a,浮动b){返回a - b;浮动Mult(浮动a,浮动b){返回a * b;浮动Div(浮动a,浮动b){返回a / b;浮动函数(float,float);

void Switch_With_Function_Pointer(float,float,float
(* pt2Func)(float,float));
int main ()

浮动a,b;
浮点(* Func)(浮点数,浮点数);
char opCode;
cout<< ; 输入数字和操作 << endl;
cin>> a>> b>> opCode;
//我真的需要这个开关吗,有更快的方法!!
开关(opCode)
{
案例''''':Func = Plus;
休息;
案例'' - '':Func =减;
休息;
案例''*'':Func = Mult;
break;
case''/'':Func = Div;
break;
}

Switch_With_Function_Pointer(a,b,Func);
返回0;
(* pt2Func)(float a,float b))
{
float result = pt2Func (a,b);
cout<< 切换功能结果是 <<结果<< endl;
}
I am trying to understand the use of Function Pointers. I wrote a
small to understand the use but am not convinced that it is the
correct implementation.
Your implementation is fine.
Can you show me a way to remove the switch statement in this case ?
Put the function pointers into a map<char,double(*)(double,double)>
and extract the required pointer using the opCode as the Key.
Thanks,
Kapil

#include "stdafx.h"
using namespace std;

float Plus(float a, float b) {return a + b; }
float Minus(float a, float b) {return a - b; }
float Mult(float a, float b) {return a * b; }
float Div(float a, float b) { return a / b; }
float Func(float,float);

void Switch_With_Function_Pointer(float,float,float
(*pt2Func)(float,float));
int main()
{

float a,b;
float (*Func)(float,float);
char opCode;
cout << "Enter the numbers and operation" << endl;
cin >> a >> b >> opCode;
// Do I really need this switch ,is there a faster way !!
switch(opCode)
{
case ''+'' : Func = Plus;
break;
case ''-'' : Func = Minus;
break;
case ''*'' : Func = Mult;
break;
case ''/'' : Func = Div;
break;
}

Switch_With_Function_Pointer(a,b,Func);
return 0;
}

void Switch_With_Function_Pointer(float a,float b,float
(*pt2Func)(float a,float b))
{
float result = pt2Func(a,b);
cout << "Switch with function result is " << result << endl;
}



这篇关于功能指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆