Use of expenevit keyword

xiaoxiao2021-03-06  37

IN C IT IS Possible to Declare Constructors for a class, Taking a Single Parameter, and use thoseconstructors for doing type conversion. For example: class a {public: a (int);}; void f (a) {} void g ) {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 Call create an A object from the integer value. Such aconstructor 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) ; // i 1 = = 67; // illegal f (77); // illegal} Using the expeneword int);}; void f (a) {} void g ) {A a1 = a (37); A A2 = A (47); 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); Reference from: http://www.glenmccl.com/expl_cmp.htm

转载请注明原文地址:https://www.9cbs.com/read-64335.html

New Post(0)