定义ULONG&的默认值。可选参数为0

时间:2013-02-11 20:49:20

标签: c++ methods compiler-errors default-parameters

以下函数声明:

void Foo:DoSomething( ULONG &Count = 0) { .... }

导致以下编译时错误

error C2440: default argument cannot convert from 'int' to 'ULONG &'

创建签名的正确方法是什么,以便在没有为Count提供参数时,其值将为零。

2 个答案:

答案 0 :(得分:2)

您正在对Count进行非const引用,因此默认情况下不能使用r值进行分配。

使用

void Foo:DoSomething( const ULONG &Count = 0) { .... }

void Foo:DoSomething( ULONG Count = 0) { .... }

答案 1 :(得分:1)

对于非const引用,您不能这样做。引用指向内存中的地址。除非你特别需要引用语义,否则我建议只传递值。