C ++ notes

Source: Internet
Author: User

1. In some cases, explicit can effectively prevent errors or misunderstandings caused by implicit conversions of constructors.
2,
Explicit only applies to constructors and is used to suppress implicit conversions. For example:
Class {
A (int );
};
Int Function (A );

When Function (2) is called, type 2 is implicitly converted to type. This situation is often not the result that programmers want. To avoid it, you can write it like this:

Class {
Explicit A (int );
};
Int Function (A );

In this way, when Function (2) is called, the compiler will give an error message (unless the Function has an overloaded form that uses int as the parameter ), this avoids errors without the programmer's knowledge.
3. The explicit keyword of the constructor effectively prevents implicit conversion from a "Bare" pointer to the auto_ptr type.
 
 
 
4. Unlike the reference counting smart pointer, auto_ptr requires its full possession of the "Bare" pointer. That is to say, a "Bare" pointer cannot be owned by more than two auto_ptr at the same time. Therefore, during the copy construction or value assignment operation, we must make special processing to ensure this feature. The auto_ptr method is "ownership transfer", that is, the source object to copy or assign values will lose ownership of the "Bare" pointer. Therefore, unlike the general copy constructor, the assign function is different, the copy constructor of auto_ptr. the parameter of the value assignment function is referenced rather than often.

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.