C ++ primer notes (7)

Source: Internet
Author: User

Suggestion: use the post operator only when necessary.
The pre-operation requires less work. You only need to add 1 and then return the result after adding 1. The post operator must first save the original value of the operand to return the result of the operation without adding 1.
Comma OPERATOR: The expression is calculated from left to right. The result of a comma expression is the value of its rightmost expression.
The new expression returns a pointer to the newly created object. We can use this pointer to access the object.
Double * Pd = new double (33 );
Delete PD;
When creating a dynamic object, (almost) always initializing it is also a good way.
Int * Pi = new int; // There is no initialization.
Int * Pi = new int (); // The default initialization function is called, where it is initialized to 0.
Const int * PCI = new const int (1024 );

Delete PCI;


The const object created dynamically must be initialized at the time of creation and its value cannot be modified once initialized.
Const string * PCs = new const string; // call the default constructor for implicit initialization.

Class objects with built-in type objects or without Default constructors must be explicitly initialized.


Three CommonProgramError (related to dynamic memory)
1. Failed to delete the pointer pointing to the dynamically allocated memory (Memory leakage ).
2. Read and Write deleted objects. After deleting the pointer, set it to 0.
3. Use two Delete expressions for the same memory space.

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.