Prayagasoft - web designer India, Ecommerce developer india, Ecommerce design

Explicit

In C++ it is possible to declare constructors for a class, taking a single parameter, and use those constructors for doing type conversion. For example:

class A { public: A(int); }; void f(A) {} void g() { A a1 = 37; A a2 = A(47); A a3(57); a1 = 67; f(77); } A declaration like:

A a1 = 37;

says to call the A(int) constructor to create an A object from the integer value. Such a constructor is called a "converting constructor".

However, this type of implicit conversion can be confusing, and there is a way of disabling it, using a new keyword "explicit" in the constructor declaration:

class A { public: explicit A(int); }; void f(A) {} void g() { A a1 = 37; // illegal A a2 = A(47); // OK A a3(57); // OK a1 = 67; // illegal f(77); // illegal } Using the explicit keyword, a constructor is declared to be

"nonconverting", and explicit constructor syntax is required:

class A { public: explicit A(int); }; void f(A) {} void g() { A a1 = A(37); A a2 = A(47); A a3(57); a1 = A(67); f(A(77)); }

Note that an expression such as:

A(47)

is closely related to function-style casts supported by C++. For example:

double d = 12.34; int i = int(d);

 

India seo freelance web designer India web development ecommerce website developer India
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100