explicit specifier
提供: cppreference.com
|
|
このページは、Google 翻訳を使って英語版から機械翻訳されました。
翻訳には誤りや奇妙な言い回しがあるかもしれません。文章の上にポインタをおくと、元の文章が見れます。誤りを修正して翻訳を改善する手助けをしてください。翻訳についての説明は、ここをクリックしてください。 |
コンストラクタと暗黙の型変換を許可していない(C++11およびそれ以降)変換演算子を指定します
Original:
Specifies constructors and (C++11およびそれ以降) conversion operators that don't allow implicit conversions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[編集] 構文
| explicit class_name ( params ) | |||||||||
| explicit operator type ( ) (C++11およびそれ以降) | |||||||||
[編集] 例
このコードを実行します
struct A { A ( int ) {} operator int() const { return 0; } }; struct B { explicit B(int) {} explicit operator int() const { return 0; } }; int main() { // A is has no explicit ctor / conversion, everything is fine A a1 = 1; A a2 ( 2 ); A a3 { 3 }; int na1 = a1; int na2 = static_cast<int>( a1 ); B b1 = 1; // Error: implicit conversion from int to B B b2 ( 2 ); // OK: explicit constructor call B b3 { 3 }; // OK: explicit constructor call int nb1 = b2; // Error: implicit conversion from B to int int nb2 = static_cast<int>( b2 ); // OK: explicit cast }