下面的函數聲明:定義爲ULONG和可選參數的默認值0
void Foo:DoSomething(ULONG &Count = 0) { .... }
結果在下面的編譯時錯誤
error C2440: default argument cannot convert from 'int' to 'ULONG &'
什麼是創造的正確方法簽名,以便當沒有爲Count
提供參數時,其值將爲零。
下面的函數聲明:定義爲ULONG和可選參數的默認值0
void Foo:DoSomething(ULONG &Count = 0) { .... }
結果在下面的編譯時錯誤
error C2440: default argument cannot convert from 'int' to 'ULONG &'
什麼是創造的正確方法簽名,以便當沒有爲Count
提供參數時,其值將爲零。
您正在對Count進行非常量引用,所以默認情況下它不能被賦值爲r值。
使用
void Foo:DoSomething(const ULONG &Count = 0) { .... }
void Foo:DoSomething(ULONG Count = 0) { .... }
不能用於非const引用做到這一點。引用指向內存中的地址。除非你特別需要引用語義,否則我建議只傳值。
@icepack抱歉。謝謝 – pondigi 2013-02-11 21:03:35