c+ - Que signifie le mot-clé explicite

Mots clés : c++constructorexplicitc++-faqexplicit-constructorc++

meilleur 5 Réponses c+ - Que signifie le mot-clé explicite

vote vote

95

class Foo { public:   // single parameter constructor, can be used as an implicit conversion   Foo (int foo) : m_foo (foo)    {   }    int GetFoo () { return m_foo; }  private:   int m_foo; }; 
void DoBar (Foo foo) {   int i = foo.GetFoo (); } 
int main () {   DoBar (42); } 
vote vote

81

class String { public:     String(int n); // allocate n bytes to the String object     String(const char *p); // initializes object with char *p }; 
String mystring = 'x'; 
class String { public:     explicit String (int n); //allocate n bytes     String(const char *p); // initialize sobject with string p }; 
vote vote

72

class Buffer { explicit Buffer(int size); ... } 
void useBuffer(Buffer& buf); useBuffer(4); 
useBuffer(Buffer(4)); 
vote vote

68

/*                                  explicit conversion          implicit conversion   explicit constructor                    yes                          no   constructor                             yes                          yes   explicit conversion function            yes                          no   conversion function                     yes                          yes  */ 
struct Z { };  struct X {    explicit X(int a); // X can be constructed from int explicitly   explicit operator Z (); // X can be converted to Z explicitly };  struct Y{   Y(int a); // int can be implicitly converted to Y   operator Z (); // Y can be implicitly converted to Z };  void foo(X x) { } void bar(Y y) { } void baz(Z z) { } 
foo(2);                     // error: no implicit conversion int to X possible foo(X(2));                  // OK: direct initialization: explicit conversion foo(static_cast<X>(2));     // OK: explicit conversion  bar(2);                     // OK: implicit conversion via Y(int)  bar(Y(2));                  // OK: direct initialization bar(static_cast<Y>(2));     // OK: explicit conversion 
X x2 = 2;                   // error: no implicit conversion int to X possible X x3(2);                    // OK: direct initialization X x4 = X(2);                // OK: direct initialization X x5 = static_cast<X>(2);   // OK: explicit conversion   Y y2 = 2;                   // OK: implicit conversion via Y(int) Y y3(2);                    // OK: direct initialization Y y4 = Y(2);                // OK: direct initialization Y y5 = static_cast<Y>(2);   // OK: explicit conversion 
X x1{ 0 }; Y y1{ 0 }; 
baz(x1);                    // error: X not implicitly convertible to Z baz(Z(x1));                 // OK: explicit initialization baz(static_cast<Z>(x1));    // OK: explicit conversion  baz(y1);                    // OK: implicit conversion via Y::operator Z() baz(Z(y1));                 // OK: direct initialization baz(static_cast<Z>(y1));    // OK: explicit conversion 
Z z1 = x1;                  // error: X not implicitly convertible to Z Z z2(x1);                   // OK: explicit initialization Z z3 = Z(x1);               // OK: explicit initialization Z z4 = static_cast<Z>(x1);  // OK: explicit conversion  Z z1 = y1;                  // OK: implicit conversion via Y::operator Z() Z z2(y1);                   // OK: direct initialization Z z3 = Z(y1);               // OK: direct initialization Z z4 = static_cast<Z>(y1);  // OK: explicit conversion 
struct V {   operator bool() const { return true; } };  struct U { U(V) { } };  void f(U) { } void f(bool) {  } 
V x; f(x);  // error: call of overloaded 'f(V&)' is ambiguous 
void print_intvector(std::vector<int> const &v) { for (int x : v) std::cout << x << '\n'; } 
print_intvector(3); 
vote vote

52

class Foo { public:     Foo(int x) : m_x(x)     {     }  private:     int m_x; }; 
Foo bar1(10);  Foo bar2 = 20; 

Questions similaires