this pointer is a C ++ class Explanation

This article explains the concept of implicit this pointer, and how to use, contains const

First Direct is given a C ++ Primer in the class, you may not fully understand, but do not worry, we have a little bit of explanation

class Sales_data {
    std::string isbn() const { return bookNo; }
    Sales_data& combine(const Sales_data&);
    double avg_price() const;
    std::string bookNo;
    unsigned untis_sold = 0;
    double revenue = 0.0;
};
//Sales_data非成员函数接口
Sales_data add(const Sales_data, const Sales_data&);
std::ostream& print(std::ostream&, const Sales_data&);
std::istream& read(std::istream&, const Sales_data&);

All members of the class must be declared inside the class , but members of the body of the function can be defined externally, such as we have written above Sales_data class, isbn function is defined in the interior, combine and avg_price function is defined in the external

Function inside the class definition is implicit inline function

inline function, that is, when calls "inline" to start the function, that is to say: when you call, but not through the mechanism through function call, the function body directly into the call at to achieve, for example, the following call

inline const string &
cout << shorter(s1, s2) << endl;
cout << {s1.size() < s2.size ? s1:s2} << endl;

"This" concept #

We look at the function isbn

std::string isbn() const { return bookNo; }

Its argument list is empty, returns a string object, how does it know which type of object is a string from?

this

Look at the example of a call

total.isbn()

When we call a member function is in fact for an object (here total) call it, isbn member Sales_data point of (bookNo), it implicitly point to members of the object of the function call

In total.isbn () call, isbn return bookNo, in fact, it implicitly return total.bookNo

Isbn member functions and by an additional implicit parameter named this visit to call it that object (this is actually pointing to the current object pointer), when we call a member function, use this function to initialize the object address this, this will point to the current object

E.g. call total.isbn (), the compiler is responsible for the total transmission address to this isbn implicit parameter can be understood as equivalent to call the compiler would be rewritten in the form

std::string isbn() const { return this->bookNo }

Because the purpose of this point is always "this" object, so this is a constant pointer (which is a top-level const, this pointer itself is constant)

isbn() const#

First you have to know the basic usage of const, how cosnt top and bottom const difference is recommended that you read this article, the following lines of code allowing you to recall the top cosnt

0 = I int; 
int const * = P1 & I; // P1 itself is constant, the top layer const 
const int = 42 is CI; // CI itself is constant, the top layer const 
const int * & P2 = CI; * after const //, p2 is constant pointer pointing to the underlying const 
const const int * p2 = P3; // look at the top left, bottom right is the look, p3 is a constant in a constant pointer pointing 
const int & r = ci; // const reference statement are bottom const, r is a constant reference

Well into the topic

Talk about the conclusion: "ISBN action () const where const is a modification of the type of this pointer implicit

First, we forget isbn, default under, this type is very version of the amount of constant pointer pointing to a class type (which is a top-level const, this pointer is that they are constant, but the object it points to is not constant), in the Sales_data member function, the default type of this is Sales_data * const

Although this is implicit, but also to follow the initialization rules, so by default we can not put this on a constant bind directly to objects , but also can not call an ordinary member function on a const object (need to use this)

Specifically, if, I mean if, if isbn is an ordinary function is not const, this is an ordinary pointer, isbn within the meaning of this will not change the subject (just return bookNo), then we should put this statement into const Sales_data * const, so this is set to point to a constant pointer improve flexibility

However, this implicit, will not appear in the parameter list, so where will this constant pointer pointing to a statement saying it? After the C ++ approach is to allow the const keyword in the parameter list member function, what we see isbn () const, this time immediately after the list of parameters const pointer to indicate this is a constant, like the use of const member functions are often referred to const member functions

// The following code is ill, only to illustrate how to use the implicit this pointer, but we can not explicitly define this pointer 
// remember this here is a pointer to a constant, because isbn is a constant member 
std ISBN SALES_DATA :: String :: (const const * SALES_DATA the this) { 
    return this-> ISBN; 
}

Define a function to return this object #

Before we declare a combine function within Sales_data

Sales_data& combine(const Sales_data&);

Now we define the function on the outside

Sales_data& Sales_data::combine(const Sales_data &rhs){
    untis_sold += rhs.untis_sold;
    revenue += rhs.revenue;
    return *this;
}

Sales_data :: combine operator to use the scope Description: We define a function called the combine, and the function declaration role SALES_DATA art class, so when using untis_sold combine and Revenue, also implicitly uses the SALES_DATA member

We call this combine

total.combine(trans)

The total addresses are bound to the implicit this parameter, and bound to the trans rhs

You should notice that this function should be concern that the return type and return statement

combine originally designed to mimic as much as possible operator + =, + =, the left side as the left operand of the return value, for as uniform as possible, must combine return a reference type (in this case the left operand is a Sales_data object, the return type as Sales_data &)

How to return it, and now we do not need to use implicit this pointer to access a specific member function of the caller, but the need to call a function of the object as a whole access

return *this;

this pointer dereferencing return statement to give the object execution of the function, total.combine (trans) returns a reference to the total

Guess you like

Origin www.linuxidc.com/Linux/2019-09/160779.htm