C + + review (not yet continued, long-term update)

Source: Internet
Author: User

OOP and the like and object 1. Introduction of this pointer

Each member function has an additional implicit formal parameter, which is the this pointer, which points to the address of the calling object. By default, the type of this is a constant pointer to a very version of the class type. Can be expressed as a pseudo-code form as follows:

/* ** Const this = &total;

This pointer is typically used to resolve duplicate name problems and return values or references. For example:

struct a{    int  A;     void Test (int  a) {        this->a = A;    }}; 

The parameter A and class member A of the test function are famous, according to the nearest principle, use a directly, call the parameter A, then how to use the masked member A, here is the this pointer. 2. Const member functions

The Const keyword immediately following the argument list acts as: Modifies the type of object to which the implicit this pointer points, as follows:

/** * CONST SALES_DATA *Const this = &total; 

The meaning of the const here is that the function cannot modify this object, in fact it is not allowed to modify the members of the class. The const plays a protective role mainly.

Note: A non-const object can call a const member function, or it can call a non-const member function, but a const object may only call the const member function. Also, non-const objects first call non-const member functions.

Finally, remember that the constructor cannot be const. If you are const, how do I complete the initialization work?! 3. The const member function and non-const member functions can form overloads.

To this end, the features that make up the function overload are: The name of the class, the function name, the function parameter list, and the Const property of the member function. In fact, the function signature is composed of these parts.

Here we explain a question: why does the C language have no function overloads? Because the compiler will maintain a symbol table when compiling the C program, the C language is simply the name of the function when it is recorded , so the function name is the unique identifier of the C function. When we try to define two functions with the same name, a redefinition occurs.

How does C + + do it? Obviously, for a normal function, its symbol ( unique identifier ) is generated from the function name and the argument list, and for the class's member functions, plus the class name and the const attribute, so when we do the function overloading, the functions in the symbol table are not the same identity. C + + It is through this mechanism that the overloading of functions is realized .

Note: the C + + compiler does not consider return values when generating function symbols, which is why function overloading and return values are irrelevant. 4. Constructor constructor initializer list (constructor initialize list)

The constructor has a special place where it can contain a constructor initialization list, as follows:

Person (intconststringint age )         : _id (ID), _name (name), _age (age) {}

In the following form, it is perfectly possible to achieve the objective:

Person (intconststringint age ) {        = id;         = name;         = Age ;}

But the two are different. The first form takes a list of constructors ' initial values, performs the actual initialization work, and the second form is the assignment operation.

Note that even if the constructor does not have a list of constructor initializers (rather, the constructor initializer list is empty), the member variables in the class will perform the default initialization. Therefore we must use the constructor default initialization list in the following cases:

A) const built-in type variable and a const class type variable that does not display a default constructor defined (you can refer to a case where the default constructor of the blog post is defined as delete)

b) Reference type member

c) class type variable with no default constructor

The essence is that const built-in type variables and reference types must be initialized, whereas for class-type objects, default constructors can be initialized by default (non-const class-type objects can be initialized by default as long as they have a default constructor). The Const class type object must have a display-defined default constructor to perform a default initialization of 5. The order in which class members are initialized is the order in which they are declared in the class, not the order listed in the initialization list

Consider the following class:

class X {    int  i;     int J;  Public :    X (int  val):    J (Val), I (j) {    }};

Our idea is to initialize J with Val, initialize I with the value of J, but here the order of initialization is first I then J.

Remember that the order in which class members are initialized is the order in which they are declared in the class, not the order listed in the initialization list! 6. Destructors

As with constructors, destructors are also a special function. Constructors are called when an object is created, and destructors are called when an object is destroyed. Constructors, like constructors, do not return a value, and the destructor does not have any parameters. As follows:

~ Person() {        }

It is important to note that:

A) The destructor for the class-type Object Foo is just the last-minute callback at the end of its life, and it doesn't control Foo's own memory, just like he can't get his own corpse.

b) For the class type object on the heap: free () The thing to do is to release the memory. The thing to do with the delete is to call the destructor, then release the memory, and note that the delete frees the memory space instead of the destructor release. For class-type objects on the stack, the destructor is called automatically when the scope is exited, and then the memory is freed.

Summary: For a class type object on a stack, like a built-in type variable, the system automatically frees the memory after the scope is exited. In fact, the difference between a built-in type object and a class-type object, whether it is a stack space or a heap space, is that the class object calls the destructor before it is destroyed.

C + + review (not yet continued, long-term update)

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.