我可以从以前的参数中设置默认参数吗? [英] Can I set a default argument from a previous argument?
本文介绍了我可以从以前的参数中设置默认参数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以将函数参数列表中先前的参数用作参数列表中后面参数的默认值?例如,
Is it possible to use previous arguments in a functions parameter list as the default value for later arguments in the parameter list? For instance,
void f( int a, int b = a, int c = b );
如果可以,有什么使用规则吗?
If this is possible, are there any rules of use?
推荐答案
答案是否定的,你不能.您可以使用重载获得所需的行为:
The answer is no, you can't. You could get the behaviour you want using overloads:
void f(int a, int b, int c);
inline void f(int a, int b) { f(a,b,b); }
inline void f(int a) { f(a,a,a); }
关于最后一个问题,C 根本不允许使用默认参数.
As for the last question, C doesn't allow default parameters at all.
这篇关于我可以从以前的参数中设置默认参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文