More efficient C ++ study note _ pointer & amp; referenc

Source: Internet
Author: User

This part is about More Effective C ++ Clauses 1 and 2. Take a note.

Bytes ----------------------------------------------------------------------------------------

 

1. pointer & reference

If there is a variable that needs to (represents) point to an object, but it is best

If a variable is used, it is best to make

Therefore

* pc = & rc = *pc;

This type of code cannot appear.

 

For pointer and reference, pointer can be null and can be re-assigned and pointed to another object. However, reference always points to its original object and cannot be re-assigned.

  s1(  s2(  & rs = s1;           * ps = &s1;          rs = s2;                   ps = &s2;                 

 

2. C ++ transformation operators

In C ++, the transformation operators include four static_cast, const_cast, dynamic_cast, and reinterpret_cast.

Static_cast is mainly used for normal type conversion, but cannot be used to remove constants or variables.

Const_cast is mainly used to change the constants of the expression. It can be used to remove the const attribute of the variable. Other operations cannot be used.

Dynamic_cast is mainly used to implement the "safe downward or cross-system transformation actions" in the inheritance system ". That is to say, you can convert a pointer or reference to a base class to a pointer or reference to a subclass. If the conversion fails, a null pointer (when the pointer is converted) or an exception (when the conversion is referenced) is returned ).

Reinterpret_cast is mainly used to convert the type of function pointers.

 

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.