我有一個函數,我通過引用傳遞了一個參數,因爲我期望函數編輯它。這個函數在幾個地方被調用,我只關心在特定實例中調用時的ref值。 僞代碼:通過引用傳遞:從'int'到'int'的參數6沒有已知的轉換
test_fn(int a, int b, inc , int d, int e, int& ref)
{
//bunch of other functionalities
//.
//.
ref = (a*b+c)*(d+e);
}
test_fn(1,2,3,4,5,0)//everywhere that I do not care about ref
int value = 0;
test_fn(1,2,3,4,5, value)//I care about value here and would use it in the remainder of the code .
我爲什麼不能直接傳遞一個0?我試着傳遞一個NULL,並且有一個int int轉換錯誤。
爲什麼這是錯的?在此實現預期結果的最佳方式是什麼?
如果您正在尋找傳遞NULL作爲一個選項,你看着使用指針在引用? http://stackoverflow.com/a/57492/1658810 –