This pointer------new standard C + + programming

Source: Internet
Author: User

Background:

C + + is developed on the basis of C language, the first C + + compiler actually translates the C + + program into a C language program, and then compiles it with the C language compiler. C language has no concept of class, only structure, function is global function, no member function. Translating a class into a struct and translating the object into a struct variable is obvious, but how should the member function of the class be translated? To "my.modify ();" How do you translate a statement that calls a member function through an object?

The C language has only global functions, so member functions can only be translated into global functions; "my.modify ();" Such statements can only be translated into ordinary statements that call global functions. So how can the post-modify global function also function on my structure variable? The answer is to introduce the "this pointer".

Example:

Here is a C + + program to C program translation.

C + + programs:

Class ccar{public    :        int price;        void Setprice (int p);}; void Ccar::setprice (int p) {    price=p;} int main () {    ccar car;    Car. Setprice (20000);}

Post-translation C program:

struct ccar{    int price;}; void Setprice (ccar* this,int p) {    this->price=p;} int main () {    struct ccar car;    Setprice (&car,20000);}

As you can see, classes are translated into structs, objects are compiled into structural variables, and member functions are translated into global functions. However, the C program's global function Setprice more than the C + + member function setprice a parameter, is "Ccar*this". "Car. Setprice (20000); " is translated into "Setprice (&car,20000);", while the latter is executed, the This parameter points to the variable of car, thus achieving the effect of the Setprice function on the car variable.

The function of this pointer:

In fact, the C + + compiler is now essentially handling member functions and calls to member functions as described above, i.e. non-static member functions actually have more formal parameters than programmers write. The extra parameter is the so-called "this pointer". The "this pointer" points to the object that the member function acts on, and in the process of executing the member function, it is through the "this pointer" that it can find the address of the object, and thus the address of all non-static member variables of the object can be found.

Example:

#incldue <iostream>using namespace Std;class a{        int i;    Public:        void Hello () {cout<< "Hello" <<endl;}}; int main () {    a*p=null:    P->hello ();}

The output of the program is:

Hello

In the above program, p is clearly a null pointer, but it can still correctly call a member function Hello, because, referring to the above C + + to C program's Pak Nai Assumption, "P->hello ()" is essentially "hello (p)", in the translated Hello function, The cout statement does not use the this pointer, so you can still output the result. If there is access to the member variable in the Hello function, the program will get an error.

The use of this pointer:

C + + Specifies that the This keyword can be used directly inside a non-static member function, and this represents a pointer to the object that the function is acting on.

Example:

#include <iostream>using namespace Std;class a{            public:double real,imga; A (double r,double i): Real (R), Imga (i) {}        A AddOne () {        this->real++;        return *this;}    ; int main () {    A C1 (), C2 (0,0);    C2=c1.addone ();cout<<c2.real<< "," <<C2.IMGA<<ENDL;}

Output is 2,1

Line 9th, the type of this pointer is a *. Because the this pointer points to the object that the function is acting on, this->real and Real are completely equivalent. "*this" represents the object that the function acts on, so the 16th line is executed, and after entering the AddOne function, "*this" is actually C1. So the value of C2 becomes the same as C1.

Attention:

Because static member functions do not work on an object, you cannot use the this pointer inside it.

New standard C + + programming

Forwarding please indicate the source

This pointer------new standard C + + programming

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.