Efficient C ++ and efficient classrooms

Source: Internet
Author: User

Efficient C ++ and efficient classrooms

Valid C ++

Pointer and reference are different:

The pointer must be initialized during definition. If this variable may point to null during use, you must use a pointer instead of reference.

Another important difference in Pointer field reference is that the pointer can be redefined to point to another different object. However, the reference always points to the specified object during initialization and cannot be changed later. If the value of the application is changed, the object to which the reference points will be modified.

In the following cases, you should use pointers. First, you may consider the possibility of not pointing to any object (in this case, you can set the pointer to null ), second, you need to be able to point to different objects at different times (in this case, you can change the pointer ). If you always point to an object and it does not change its direction once it points to an object, you should use references.

When you know that you must point to an object and do not want to change its direction, or when you overload operators and prevent unnecessary semantic misunderstanding, you should not use pointers. In other cases, the pointer should be used.

 

Type conversion in C ++:

Const_cast: used to remove the const attribute; static_cast: Forced type conversion of the type in C.

Dynaminc_cast: used for conversion between the base class and the inherited class:

Reinterpret_cast: generally used for conversion between function pointer types.

 

Do not use polymorphism for Arrays:

The most important feature of class inheritance is that you can operate a derived class through a base class pointer or reference. Such a pointer or reference has a behavioral polymorphism. However, it is incorrect to use Arrays for polymorphism. The implementation of the structure is also incorrect.

 

Implicit type conversion constructor. For example, to avoid parameter constructor and built-in data type conversion, you need to use the keyword explicit

 

Differences between the prefix form and suffix form of auto-increment and auto-increment operators:

C ++ allows you to overload the increment and decrement operators. The differences between overload functions depend on the differences in their parameter types. However, no matter the prefix or suffix of Increment and decrment, there is only one parameter. To solve this language problem, C ++ requires an Int type parameter in the form of a suffix. When a function is called, the compiler passes a value 0 as the Int parameter to the function. One response reference and one response temporary variable. The suffix operator function does not use its parameters. Its parameters are only used for calling the partition prefix and suffix function.

 

The Placement new operation initializes an object in an existing memory. For example, if you already have a heap space, you want to initialize an object in this heap space, you can use this operation.

 

The default constructor can be called without any real parameters. Such a constructor does not have any parameters, or each parameter has a default value. Some class constructors are declared as explicit, which prevents them from being used for implicit type conversion, but they can still be used for display type conversion. The compiler is prohibited from performing unexpected type conversions. The Copy constructor is used to "initialize the self-object with the same object", and the copy asignment operator is used to "Copy its value from another same object to the self-object ".

Widget w1; // call the default constructor

Widget w2 (w1); // call the copy constructor

W1 = w2; // call the copy assignment operator

Be careful when you see the value assignment symbol, because the "=" syntax can also be used to call the copy constructor:

Widget w3 = w2; // call the copy constructor

The Copy structure differs from the copy value assignment. If a new object is defined (for example, w3 of the preceding statement), A constructor is called and it is impossible to call the value assignment operation. If no new object is defined (for example, w1 = w2), no constructor will be called, and the value assignment operation will be called.

The Copy constructor is a particularly important function because it defines how an object is passed by value (passed by value). When a function parameter is of a class type, the copy constructor is called during the real parameter passing process. passed-by-value indicates that the copy constructor is called.

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.