C / C ++默认参数设置为previous参数 [英] C/C++ default argument set as a previous argument

查看:151
本文介绍了C / C ++默认参数设置为previous参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法找到一个明确的答案,这虽然可能是因为我没有使用正确的搜索条件,请重定向我,如果是这样的话。是否有可能在一个函数参数列表在参数列表后参数的默认值使用previous参数呢?例如,

I was unable to find a clear answer to this although that might be because I was not using the proper search terms so please redirect me if that is the case. 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 );

如果这是可能是使用有什么规则? C和C ++之间有什么区别?

If this is possible are there any rules of use? Any difference between C and C++?

推荐答案

答案是否定的,你不能。你可以得到的行为要使用重载:

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.

这篇关于C / C ++默认参数设置为previous参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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