C + + Base __c++

Source: Internet
Author: User

Delete p; Just remove the pointer p pointing to the memory area, not delete the pointer p, so p is still available. It is OK to remove the memory pointed to by the null pointer.

Variables and objects in the heap are anonymous, have no names, and can only be accessed through pointers.

When an object is created in the heap, the constructor of the class is invoked while the memory is allocated, and the class's destructor is invoked when the object in the heap is deleted.

To avoid memory leaks, you should assign the value of a pointer to 0 after you delete it.

The constant pointer is the memory area address that the pointer points to cannot be changed, but the value stored in the memory address can be changed, such as int A; int * Const P = &a;

A pointer to a constant indicates that the object that the pointer is pointing to cannot be modified, but the pointer can be modified, that is, the pointer can point to another destination memory address. such as const int a = 0; const int p = &a; If a is a class, it can also be a const a P = new A;

A regular pointer to a constant indicates that the pointer itself cannot be modified, and the contents of the memory address that it points to cannot be modified. such as const int a = 0; const INT * Const P = &a;

A reference is an alias constant.

The addresses in the heap are manipulated with pointers, and aliases are not used.

If the declarations and definitions of the other functions in front of the main function are together, this function is an inline function. So when the function is longer, you should separate the declaration from the definition.

Multiple values can be returned by pointer or reference, because the return mechanism can only return one value, so other return values are passed as arguments, by reference or pointer. If the object is large when passed by value, the system overhead is large (for example, the object is passed in and returned, a temporary object is created 2 times, and therefore the constructor and destructor are called more than once), which is typically passed by address or by reference.

Since the reference implements the function of the pointer and is more convenient to use, why do you want pointers?

This is because the pointer can be empty, but the reference cannot be empty, the pointer can be assigned, but the reference can only be initialized and cannot be assigned an alias to another object. If you want to use a variable to record the address of a different object, you must use a pointer, and the pointer can also be multiple.

Pointers can point to space in the heap, and references cannot point to space in the heap. However, pointers and references can be used together, in addition to the life cycle of references.

For a reference, if the reference is a temporary variable, then the lifetime of the temporary variable is not less than the lifetime of the reference. The pointer does not have this feature.

To avoid memory leaks, we cannot return an object in a heap by value, but must return an alias or memory address by address or alias, so that the copy constructor is not invoked to create a copy of the object, but rather to return the object's alias or address directly.

To avoid pointer confusion, we have to release the function in which the memory in the heap is created.

If the function name is the same, the number of function arguments is the same, but the parameter is of different types, it can also be a function overload.

If the function has a default value, the parameter can be passed off at the time of the call, which is less than the number of functions.

The default argument can actually be considered a function overload, but a function of the default parameter is easily ignored if it is not annotated, and is easily overwritten by a function with the same name as a parameter. The usual overloaded functions are easy to use and easy to understand. Overloads with default parameters are numeric values of the parameters, while overloaded functions overload the type of the parameter.

I can initialize constants and references to the function headers of constructors, at which point the initialization order is in the order of the list of members, not the order in which the constructor headers are assigned. And the sequence of the deconstruction is the exact opposite.

In general, the compiler automatically generates a default copy constructor for the class.

Clear the combination of constructors and new, destructor and delete.

Constructors cannot be set to private.

The default constructor is a shallow constructor, and if the member variable in the class has pointers, there is a good chance of a memory leak error, so you need to use the deep constructor at this point.

The copy constructor is invoked when an object is created with an existing object. If the left side of the replicator is an existing object and an existing object is on the right, the copy constructor is not invoked, but an assignment operator function is invoked.

As soon as you create a class, the compiler automatically adds 4 functions: constructors, destructors, copy constructors, assignment operator functions, where the system default copy constructors and replication operators belong to shallow copies.

The default custom overload operator is the forward self operator.

You can do this by creating an unnamed temporary object to perform an overloaded, self-added object assignment operation. Of course, a better approach is to return the *this pointer so that you don't need to create a temporary object, but it still calls the assignment operator, and to avoid this, you can set the overloaded self-add function return value type to the reference of the returned object.

When overloading the post-add operator, in order to distinguish the difference from the predecessor, a meaningless argument needs to be added, which is used only in the compiler and is useless in the function body.

The order in which the operators are executed in one statement is Right-to-left, and the different compiler stacks are in different order, for example, the VC6.0 in the stack is required after the expression of the statement is executed. So a=1;cout<

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.