在具有多个参数的 C++ 函数中,我希望其中一个参数具有默认值,该默认值本身就是其他参数的函数。例如,
int f1( int m );
int f2( int n1, int n2 = f1( n1 ) ) {
// Do stuff with n1 and n2
}
这不会编译,但希望它能明确我想要的函数 f2 的行为。它的调用者应该能够手动向其传递 n2 的值,但默认情况下,n2 的值应通过在 n1 上调用 f1 来确定。对于如何最好地实现(或至少近似)此行为有哪些建议?
改为重载该函数:
int f1( int m );
int f2( int n1, int n2 ) {
// Do stuff with n1 and n2
}
int f2( int n1 ) {
return f2( n1, f1( n1 ) );
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)