我有一个C++函数,它有5个参数,所有参数都有默认值。如果我传入前三个参数,程序将为最后两个参数分配一个默认值。有什么办法可以传递3个参数,并在中间跳过一个参数,给出值,说第一个,第二个和第五个参数?在C++函数中跳过一些参数?
8
A
回答
0
不,这是不可能的。
但是我建议你应该使用参数的数据类型数组来代替你实现的场景。 您也可以重载。如果参数的数据类型不同,您应该定义一个类,它具有所需参数作为成员。传递该类的对象。它不仅可以解决您的问题,而且还可以从可维护性的角度来推荐。
5
不是直接的,但你也许能够做一些事情的std ::绑定:
int func(int arg1 = 0, int arg2 = 0, int arg3 = 0);
// elsewhere...
using std::bind;
using std::placeholders::_1;
auto f = bind(func, 0, _1, 0);
int result = f(3); // Call func(0, 3, 0);
缺点当然是您要重新指定默认参数。我相信别人会有更聪明的解决方案,但如果你真的很绝望,这可能会奏效。
1
使用经典的5参数函数,没有办法给它只有3或4.你只能用默认参数写入3或4,但最后你将得到一个带有5个参数的函数调用。
如果存在几个相同类型的参数,那么系统也存在问题。 例如,如果您有foo(int a=4,int b=5)
并致电foo(10)
,您如何知道您要拨打foo(10,5)
或foo(4,10)
?
随着C++ 11元组和Named parameters idiom,你可以作弊一点点。
#include <iostream>
#include <functional>
#include <tuple>
#include <string>
struct f_
{
private:
typedef std::tuple<int,int,double> Args;
//default arguments
static constexpr const Args defaults = std::make_tuple(10,52,0.5);
Args args;
public :
f_():args(defaults)
{}
template <int n,class T> f_& setArg(T&& t)
{
std::get<n>(args) = t;
return *this;
}
void operator()()
{
return (*this)(std::move(args));
}
void operator()(Args&& a)
{
int n1=std::get<0>(a);
int n2=std::get<1>(a);
double n3=std::get<2>(a);
std::cout<<n1<<" "<<n2<<" "<<n3<<std::endl;
}
};
#define set(n,v) setArg<n>((v))
int main()
{
//f_().set<1>(42).set<3>("foo")();
f_().setArg<1>(42)(); //without Macro
f_().set(0,666).set(1,42)(); //with Macro
f_()(); //without any parameters
f_()(std::forward_as_tuple(-21,-100,3.14)); //direct call
}
的另一种方法是使用std ::绑定描述there
-2
也许这是东西,你可能会寻找,只是一个解决办法!
/*
Function f() is called by passing 2 arguments. So to make sure that these 2 arguments are treated as
first and third, whereas the second and the fourth are taken as defaults:
SOLUTION 1 : using recursive call
*/
#include <iostream>
using namespace std;
void f(int = 10,int = 20, int = 30, int = 40);
static int tempb;
static int flag = 1;
int main()
{
cout << "calling function \n";
//f();
f(12,39);
}
void f(int a,int b,int c,int d)
{
//static int flag = 1;
//f();
if(flag == 1)
{
--flag;
f(); //recursive call to intialize the variables a,b,c,d as per the prototype
c = b;
b = tempb;
//cout << c;
}
else
{
tempb = b;
return;
}
cout << endl <<"a = " << a << endl << "b = "<< b << endl << "c = " << c << endl << "d = " << d << endl;
}
以下是另一种解决办法可能是这样也有帮助!
/*
Function f() is called by passing 2 arguments. So to make sure that these 2 arguments are treated as
first and third, whereas the second and the fourth are taken as defaults:
SOLUTION 2 : using static variable
*/
#include <iostream>
using namespace std;
void f(int = 10,int = 20, int = 30, int = 40);
static int tempb;
int main()
{
f();
f(12,39);
}
void f(int a,int b,int c,int d)
{
static int flag = 1;
if(flag == 1)
{
--flag;
tempb = b;
return;
}
else
{
c = b;
b = tempb;
}
cout << "a = " << a << endl << "b = " << b << endl << "c = " << c << endl << "d = " << d;
}
+1
我不能去全局静态变量来选择方法参数。没有人应该使用这种方法。 – Brannon
相关问题
- 1. C++跳过一个函数?
- 2. 跳过一个函数参数?
- 3. 正在跳过C++函数
- 4. 根据参数跳过一些测试
- 5. 编译器在C++中跳过可变参数模板/函数
- 6. 可变参数函数跳过第一个参数
- 7. xargs sh -c跳过第一个参数
- 8. 在JavaScript中跳过参数
- 9. Javascript跳过参数函数调用
- 10. 跳过函数参数的JavaScript
- 11. 跳过一个onblur函数
- 12. C++模板函数跳过一个静态函数调用
- 13. C++通过参数传递函数中的另一个函数参数
- 14. AJAX呼叫跳过发送一些参数或PHP失去其中一些
- 15. Codeigniter函数跳过
- 16. C#foreach通过跳过一些值
- 17. 如何跳过默认参数C++?
- 18. 如何在wordpress函数中跳过某些类的图像
- 19. 从对象轨跳过一些数据
- 20. 计划被跳过函数getline()/ C++
- 21. 跳过C++全局析构函数
- 22. Howto不能跳过python中的字符串函数参数
- 23. String getline(cin,variable)函数跳过一些代码行
- 24. 在C++中将函数参数作为函数参数传递
- 25. 让@lru_cache忽略一些函数参数
- 26. C++:通过模板传递参数Vs通过函数参数
- 27. LESS.js + @arguments +跳过一个参数
- 28. python。通过函数指定了一些参数
- 29. 一些C#中的可选参数
- 30. *在函数参数(C)
简答:第 –
有解决方法。但是如果可以的话,那就是超载。 – jrok