bja*_*fly 7 c++ overloading default-arguments
我有一个关于对象中的重载函数的默认值的问题.
如果我有一个函数签名,如下所示,默认值只评估一次或每次?
class X
{
public:
f(const RWDate& d=RWDate::now());
}
// when calling f() do I get the current time each time?
X z;
z.f();
// is the default value of d recaculated in the function call?
z.f();
Run Code Online (Sandbox Code Playgroud)
Jam*_*lis 12
默认参数在调用站点被替换,因此z.f()
被转换为
z.f(RWDate::now())
Run Code Online (Sandbox Code Playgroud)
因此,每次调用函数并使用默认参数时,都会计算默认参数.