Efficient C + +--experience clause (i)

Source: Internet
Author: User

Effective C + +

Pointers and references are different:

The pointer must be initialized at the time of definition, and if the variable may point to null during use, do not use a reference, you need to use a pointer.

Another important difference in pointer field referencing is that pointers can be redefined to point to another different object. But the reference always points to the object that was specified at initialization and cannot be changed later, and if the applied value is changed, then the object to which the reference is directed is modified.

You should use pointers when you consider the possibility of not pointing to any object (in which case you can set the pointer to null), and you need to be able to point to different objects at different times (in which case you can change the pointer's direction). If you always point to an object and do not change the point once it points to an object, then you should use a reference.

You should not use pointers when you know that you must point to an object and do not want to change its point, or if you overload the operator and are misunderstood to prevent unnecessary semantics. In other cases, you should use pointers.

Type conversions in C + +:

Const_cast: Specifically used to remove the const attribute; static_cast: coercion type conversion in C.

Dynaminc_cast: Used to convert between a base class and an inherited class:

Reinterpret_cast: Typically used to convert between function pointer types.

Do not use polymorphism for arrays:

The most important attribute of class inheritance is that you can manipulate derived classes by using a base-class pointer or reference. Such a pointer or reference has the polymorphism of the behavior. But it is wrong to use arrays to achieve polymorphism. It is also wrong to implement the destructor.

Implicit type conversion constructors, such as avoiding but conversion of parameter constructors and built-in data types, require the use of the keyword explicit

The difference between the prefix form of the self-increment and decrement operator and the suffix form:

C + + allows two forms of overloaded increment and decrement operators. The difference between overloaded functions depends on the difference in their parameter types, but the prefix or suffix of increment and decrment is only one argument. To solve this language problem, C + + specifies that the suffix form has an int type parameter, and when the function is called, the compiler passes a value of 0 as the int parameter to the function. A return reference, one that returns a temporary variable. The postfix operator function does not use its arguments, and his arguments are only used to partition the prefix with the suffix function call.

The Placement new operation is to initialize its own object in an existing memory, such as you have a heap of space, you want to initialize an object in this heap space, you can use this operation.

The so-called default constructor is a person who can be called without any arguments. Such constructors do not have parameters, or each parameter has a default value. The constructors of some classes are declared as explicit, which prevents them from being used to perform implicit type conversions, but they can still be used for display type conversions. He prohibits the compiler from performing unintended 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 object of the same type to the Self object".

Widget W1; Call the default constructor

Widget W2 (W1); Call the copy constructor

W1 = W2; Invoking the Copy assignment operator

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

Widget W3 =w2; Call the copy constructor

Copy construction differs from copy assignment. If a new object is defined (for example, the W3 of the above statement), there must be a constructor called and the assignment operation cannot be invoked. If no new object is defined (for example, w1 = W2 above), no constructor is called, then of course the assignment operation is called.

The copy constructor is a particularly important function, because it defines how an object is passed by value (passed in values), when the argument of a function is a class type, the argument passing procedure calls the copy constructor, and passed-by-value means that the copy constructor is called.

Efficient C + +--experience clause (i)

Related Article

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.