好好利用fn ptrs? [英] A good use of fn ptrs?
问题描述
这是fn ptrs的好用吗?
#include< iostream>
#include< cstdlib>
using namespace std;
typedef void(* v_v_fptr)();
void Error(){cout<< "错误" << endl;}
void Success(){cout<< "成功" << endl;}
v_v_fptr select(bool arg){if(arg == true)return success;否则返回
错误;}
int main()
{
bool val;
cout<< 1 | 0?: << endl;
cin>> val;
v_v_fptr ptr = select(val);
ptr();
system(" PAUSE");
返回0;
}
或者我应该使用fn objs代替吗?谢谢!!!
Is this a good use of fn ptrs?
#include <iostream>
#include <cstdlib>
using namespace std;
typedef void (*v_v_fptr)();
void Error(){cout << "Error" << endl;}
void Success(){cout << "Success" << endl;}
v_v_fptr select(bool arg){if(arg==true)return Success; else return
Error;}
int main()
{
bool val;
cout << "1|0?: " << endl;
cin >> val;
v_v_fptr ptr=select(val);
ptr();
system("PAUSE");
return 0;
}
Or should I use fn objs instead? Thanks!!!
推荐答案
Protoman写道:
Protoman wrote:
这是一个很好用的fn ptrs吗? br />
....
或者我应该使用fn objs代替?谢谢!!!
Is this a good use of fn ptrs?
....
Or should I use fn objs instead? Thanks!!!
fn objs更具扩展性,你什么都没有。
fn objs are more extensible and you loose nothing.
我该如何制作它用fn objs?像这样:
班级选择
{
public:
v_v_fptr operator()(bool val){if(val == true)返回成功;否则返回
错误;}
};
// ...
v_v_fptr ptr = select()(val);
ptr();
你能帮忙吗?谢谢!!!
How would I make it use fn objs? Like this:
class select
{
public:
v_v_fptr operator()(bool val){if(val==true)return Success;else return
Error;}
};
//...
v_v_fptr ptr=select()(val);
ptr();
Could you help? Thanks!!!
我用C ++编程了10多年但是我没有看到这样的
a方式的错误检查。但在语法上它是正确的。如果我是你,我将使用例外。
Protoman写道:
I am programming for more that 10 years in C++ but I have not seen such
a way of error checking. But syntatically it is correct. If I were you I
would use exceptions.
Protoman wrote:
这是一个很好用的fn ptrs吗?
#include< iostream>
#include< cstdlib>
使用命名空间std;
typedef void(* v_v_fptr)();
void Error(){cout<< "错误" << endl;}
void Success(){cout<< "成功" << endl;}
v_v_fptr select(bool arg){if(arg == true)return success;否则返回
错误;}
int main()
{
bool val;
cout<< 1 | 0?: << endl;
cin>> val;
v_v_fptr ptr = select(val);
ptr();
system(" PAUSE");
返回0;
}
或者我应该使用fn objs代替?谢谢!!!
Is this a good use of fn ptrs?
#include <iostream>
#include <cstdlib>
using namespace std;
typedef void (*v_v_fptr)();
void Error(){cout << "Error" << endl;}
void Success(){cout << "Success" << endl;}
v_v_fptr select(bool arg){if(arg==true)return Success; else return
Error;}
int main()
{
bool val;
cout << "1|0?: " << endl;
cin >> val;
v_v_fptr ptr=select(val);
ptr();
system("PAUSE");
return 0;
}
Or should I use fn objs instead? Thanks!!!
这篇关于好好利用fn ptrs?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!