C + + Primer Learning Note _16_ class and Data Abstraction (2) _ Implied this pointer

Source: Internet
Author: User

C + + Primer Learning Note _16_ class and Data Abstraction (2) _ Implied this pointer

1. Introduction

As mentioned earlier, the member function has an additional implicit parameter, which is a pointer to the class object. This implicit parameter is named this.


2. Return to *this

The member function has an implied additional formal parameter, which is a pointer to the object, which is called the This pointer (which is automatically passed by the compiler), which guarantees that each object can have data members of different values, but the code that handles those members can be shared by all objects. The member function is a read-only code that is shared by all objects and does not occupy the storage space of the object, because the this pointer points to the current object, so the member function can distinguish which object it is acting on.


3. Return from const member *this

In a normal non-const member function , the type of this is a const pointer to the class type . You can change The value pointed to by this , But you cannot change the address saved by this. in the const member function, the type of this is a const pointer to the Const class type Object . You can neither change the object that this is pointing to nor change the address saved by this.

cannot be from Const the member function returns a generic reference to the class object . If display is used as a const member of screen, the this pointer inside the display will be a const of type constscreen*. However:

   Myscreen.move (4,0). Set (' # '). Display (cout);   OK   Myscreen.display (). Set (' * ');    Error


4. Variable data members

Sometimes we want the data members of the class (even in the const member function) to be modified. This can be achieved by declaring them as mutable.

Mutable data members can never be const, even when they are members of a const object. Therefore, the const member function can change the mutable member.

Class Screen{public:   //... private:   mutable size_t access_ctr;    Use ACCESS_CTR to track the call frequency of the screen member function   void Do_display (std::ostream &os) const    {       + + access_ctr;      OK       os << contents;    }};

"Practice: Look at a classic topic"

Class A{public:    int m;    Voidprint ()    {       cout << "A" << Endl;    }; A *pa = 0;pa->print ();

Equivalent to the member function passed the this pointer is 0, that the call will be an error? The "a" must be correctly output, because this is 0 to indicate that an object is not being manipulated, and print does not operate on an object member, so it can be run.



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C + + Primer Learning Note _16_ class and Data Abstraction (2) _ Implied this pointer

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.