【发布时间】:2015-11-05 05:52:54
【问题描述】:
是否有可能实现这样的功能,如果不指定,该参数的值将默认为另一个参数的值?
例子:
class Health
{
public:
// If current is not specified, its value defaults to max's value
Health(int max, int current = max) : max_(max), current_(current) { }
int max_;
int current_;
};
现在,我收到一个编译错误:
error: 'max' was not declared in this scope
Health(int max, int current = max) : max_(max), current_(current) { }
^
【问题讨论】:
标签: c++ parameters