C + + supplements--this pointers

Source: Internet
Author: User

C + + supplements--this pointers

Preface

When using C + + for object-oriented programming, this pointer is something that everyone knows. But do we really know about it? Here we discuss the significance of its existence and how to use it.

This pointer

Existence Meaning: Why do we need this pointer?

To see a simple example

#include <iostream>using namespace Std;class myclass{protected:int a;public:myclass (int a): A (a) {}void SetA (int a {this->a = A;} int Geta () const{return A;}; int main () {MyClass my1 (0);  MyClass my2 (1); My1.seta (2); My2.seta (3); cout << "my1::a =" << My1.geta () << endl;cout << "my2::a = "<< My2.geta () << endl;cin.get (); return 0;}
Run



This example is simply too simple! Everyone can see with their toes. We need to think deeply about:

There is no this parameter in the 1.setA method, why can this be used in the method body?

A: The This parameter is implicitly defined, which represents the memory address of the current object. We do not write, does not mean that it does not exist.

2. When designing the Seta method, I deliberately designed the method parameter (int a) and the class member variable (int myclass::a) to have the same name. So that the function body must be written this->a=a; to complete the assignment. If the formal parameter is written in a different name, such as int A, can I omit this?

Answer: Yes. But in essence it is this->a=a; or the sentence: We do not write, does not mean that it does not exist. When compiling, the compiler automatically changes "A=a" to "this->a=a;".

3. From the above two questions have not yet clarified the existence of this meaning? That said, the program in memory storage principle: code area is public, static variables are public . There is only one part of the public meaning.

The former is the public reason: save memory. The latter is the public reason: logically it should be so! (The existence meaning of static variables)

In summary, any object of class MyClass calls the same code when calling the Seta method. But why do they have different effects? The My1 object is assigned to my1.a when it calls Seta. The My2 object is assigned to my2.a when it calls Seta. They execute the same code, but they don't mess up because the this pointer exists!

Is that it? Not yet, we need to point out further: the this pointer is implicitly defined in the method parameter. So where is it? This is not a good thing to say, my guess: the first parameter. My reasons: When the method call, the arguments passed in from right to left into the stack, when the stack, the first position of course the parameter is the first out of the stack. (If you have a different view, welcome to the discussion!) The following discussion is based on this hypothesis.

So, SetA (int a); Essentially, it should be SetA (MyClass * const this, int a);

My1.seta (2); essentially My1.seta (&my1, 2);

Further, careful you will find that I write MyClass * const This, not MyClass * This, more a const, why? There's a reason for that, of course!

The reason is simple: if this is not const, then you can change the direction of this, as in the following code:

void SetA (int a) {this = &my;this->a = A;}
Of course, such code must not be compiled. Each call to Seta becomes an assignment to the my.a.

Summary: By default, the type of this is a constant pointer to a very version of the class type . ("C++primer")


Not yet, we have to go further ...

What is the const member function? Does it have a connection with this?

We know that the Const member function is not allowed to modify member variables. And how did it do it?

Cause: The modification of a const member function is the modification of this.

The above example is an int geta () const, which is essentially an int geta (MyClass const * const);


At this point, the truth is finally revealed.







C + + supplements--this pointers

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.