C ++-explicit usage

Source: Internet
Author: User

 

Explicit

C ++ Specific

 

This keyword is a declaration specifier that can only be applied to in-class constructor declarations. Constructors declared explicit will not be considered for implicit conversions. For example:

 

Class X {

Public:

Explicit X (int); // legal

Explicit X (double) {// legal

//...

}

};

 

Explicit X: X (int) {}// illegal

An explicit constructor cannot take part in implicit conversions. It can only be used to explicitly construct an object. For example, with the class declared above:

 

Void f (X ){}

Void g (int I ){

F (I); // will cause error

}

Void h (){

X x1 (1); // legal

}

The function call f (I) fails because there is no available implicit conversion from int to X.

 

Note It is meaningless to apply explicit to constructors with multiple arguments, since such constructors cannot take part in implicit conversions.

 

End c ++ Specific

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.