C默认功能参数

我想实现这个目标:
- second parameter by default set to first argument

就像是:

int foo (int a,int b = a);

但是怎么做呢?

非常感谢!

解决方法

这是禁止的:

8.3.6认参数[dcl.fct.default]

9) Default arguments are evaluated each time the function is called.
The order of evaluation of function arguments is unspecified.
Consequently,parameters of a function shall not be used in default
argument expressions,even if they are not evaluated.
Parameters of a
function declared before a default argument expression are in scope
and can hide namespace and class member names. [ Example:

int a;

int f(int a,int b = a); / / error: parameter a

/ / used as default argument

typedef int I;

int g( float I,int b = I (2)); / / error: parameter I found

int h(int a,int b = sizeof (a )); / / error,parameter a used

/ / in default argument

—end example ]

另一种方法是重载:

int foo(int a,int b);

int foo(int a)
{
   return foo(a,a);
}

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...