This pointer to the C + + learning Note

Source: Internet
Author: User

To illustrate this problem, start by creating a simple class

1#include <iostream>2#include <string>3 4 using namespacestd;5 6 class Book7 {8 Private:9     stringtitle;Ten     intYear ; One     DoublePrice ; A  Public: -     DoubleGetPrice ()Const{returnPrice ;} -}

This class is a simple representation of a book's information, which allows the program to view a series of books, find the one with the lowest price, and then return the information for that book. This approach is to define a member function that looks at two book objects and returns a reference to that object with the lower price. But when you implement this approach, there are some interesting questions.

First, how do you provide two objects to compare to member functions? By adding the method named Lowprice (), declaring two objects book Book1 and book Book2, the function call Book1.lowprice () will access the Book1 object data, and Book2.lowprice () will access the Book2 object data. If you want the method to compare two objects, you must pass the second object as a parameter to it. Parameters can be passed by reference (high efficiency), i.e. the Lowprice () method uses a parameter of type Const book &, where the const qualifier means that the value of the parameter is not changed during the execution of the method.

Secondly, how to put the answer to the method (that is, the lower price book object, here do not think that only return the price, because we want to be a lower price of the books all the information, so to return the object) passed back to the calling program? The most straightforward approach is to have the method return a reference to the object of the lower-priced book. Therefore, the method used for comparison is prototyped as follows:

Const Book & Lowprice (constconst;

Here so many const, do not faint, if dizzy, it is recommended to study the use of the Const qualifier, here I simply explained that the first const indicates that the function returns a reference is a constant (one of two book objects), The const preceding the parameter indicates that the value of the parameter is not allowed to be modified while the program is running, and the const after the method parameter list means that the method does not allow the object's member variable value to be modified, that is, it compares the constant to return a constant, but does not have permission to modify any value.

Suppose you want to compare two object book Book1 and book Book2, and assign one of the lower prices to the low object (book low), use one of the following statements:

1 low = Book1.lowprice (book2); 2 low = Book2.lowprice (Book1);

Form has, now is to pay attention to the implementation of Lowprice (), it will raise a small problem. The following part of the implementation highlights this issue:

1 Const Book & Book::lowprice (constconst2{3     if (B.price < Price ) 4         return b; 5     Else 6         return ???? ; 7 }    

Why is that? If B.price is less than price, returns a reference to B, otherwise, returns the object used to invoke the method, the question is, how do you address the object? (This sentence must be understood, otherwise you will not see the problem)

Well, the this pointer is out,

    • All class methods set the this pointer to the address of the object that called it, such as: Book C; This will point to the address of object C
    • The this pointer is passed as a hidden argument to all methods in the class;
    • The price in the GetPrice () method above is simply a shorthand for this->price
    • Each member function, including constructors and destructors, has a this pointer. This points to the calling object, and if the method needs to refer to the entire object, you can use the expression *this, because this is the address of the object, so the object itself is *this.

*this can now be used as an alias for the calling object to complete the preceding definition:

Const Book & Book::lowprice (const-const{    if (B.price < price )         return b;     Else        return *this;}    

The return type is a reference, which means that the calling object itself, not its copy, is returned.

01:18:36

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.