【前言】 今天遇到了個(gè)問題,被人問及 explicit 作用的時(shí)候,想不起來了 【正文】 簡單的說,就是類的構(gòu)造函數(shù)禁止隱式類型轉(zhuǎn)換。先看一看微軟大哥是如何說的: This keyword is a declaration specifier that can only be applied to inclass constructor declarations. Constructors declared explicit will not be considered for implicit conversions. For example: An explicit constructor cannot take part in implicit conversions. It can only be used to explicitly construct an object. For example, with the class declared above: The function call Note It is meaningless to apply explicit to constructors with multiple arguments, since such constructors cannot take part in implicit conversions. 總結(jié)一下,大致意思就是: (1) explicit 只能在類的內(nèi)部使用,不能在外面聲明。 (2)聲明了explicit以后,就不允許隱式轉(zhuǎn)換了。 (3)explicit對于多個(gè)參數(shù)的構(gòu)造函數(shù)無任何意義。 |
|