不準確地回答你的問題,但一般來說,要表明可選的返回值,請考慮以下事項。
對於情況下,可選的返回值是有道理的,你可能會考慮boost::optional
,和C++ 14將有可能定義std::optional
(基於增壓庫)。在diguise表示成功
附加返回值,如bool
S,經常會在你不能只有在情況下,你可以初始化對象的負擔,如果你想返回一個可選的值,對於這個有沒有默認的構造,你不能,或者你必須傳遞引用指針,然後必須在被調用的站點上檢查指針等。:
std::optional<Intensity> sample_spectrum (float); // clean and concise
-------------------------------------------------------------------------
bool sample_spectrum (Intensity &out); // either not possible or you
// have a wasted initialization
-------------------------------------------------------------------------
bool sample_spectrum (Intensity *& out) { // now you have two problems
if (!out) throw std::logic_error(""); // I
....
}
....
try { .... } catch (...) { .... } // II
-------------------------------------------------------------------------
Intensity *sample_spectrum(float) ; // please no
-------------------------------------------------------------------------
std::shared_ptr<Intensity>
sample_spectrum(float); // just use std/boost::optional
ClassType是如何定義的? ,我; m對構造函數感興趣? – Raxvan
@Raxvan您認爲的隱式轉換? – Nik
@Nik是,隱式轉換。 – Raxvan