explicit specifier
Da cppreference.com
![]() |
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
Especifica construtores e operadores de conversão (desde C++11) que não permitem conversões implícitas
Original:
Specifies constructors and (desde 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.
[editar] Sintaxe
explicit class_name ( params ) | |||||||||
explicit operator type ( ) (desde C++11) | |||||||||
[editar] Exemplo
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 }