Class this pointer const member function

Source: Internet
Author: User
C ++ primer Chapter 1 class
7.1.2

? The sales_data class is defined as follows:

 
 
  1. #ifndef SALES_DATA_H
  2. #define SALES_DATA_H
  3. #include <string>
  4. #include <iostream>
  5. class Sales_data {
  6. public:
  7. std::string isbn() const {return bookNo;}
  8. Sales_data& combine(const Sales_data&);
  9. double avg_price() const;
  10. private:
  11.     std::string bookNo;
  12. unsigned units_sold = 0;
  13. double revenue = 0.0;
  14. };
  15. Sales_data add(const Sales_data&, const Sales_data&);
  16. std::ostream &print(std::ostream&, const Sales_data&);
  17. std::istream &read(std::istream&, const Sales_data&);
  18. #endif

Note Row 3:

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

Here, const is used to modify the implicit this pointer type. By default, this is a constant pointer pointing to a very large version of the class type. For example, in the sales_data member function, the type of this is sales_data * const, that is, once the class instantiates an object, this points to this object and cannot be changed, but the object itself can be changed (in general, the address saved by this cannot be changed, but the content pointed to by * this can be changed, so * this is not a constant, this cannot be bound to a constant object.) For example:

 
 
  1. Const int A = 5; // A is an integer constant
  2. Int * const Pa = & A; // PA is a constant pointer, but * pA can be changed, but a is a constant, so an error is returned.

So this will bring about a problem: we cannot call common member functions on a constant object. The test is as follows:

Remove const and the test code is as follows:

 
 
  1. #include "Sales_data.h"
  2. int main()
  3. {
  4. Sales_data data1;
  5. std::cout << data1.isbn() << std::endl;
  6. return 0;
  7. }

The result is OK. data1 is non-constant. It is no problem to call a common member function. The following is a change to declare data1 as a constant.

 
 
  1. #include "Sales_data.h"
  2. int main()
  3. {
  4. const Sales_data data1;
  5. std::cout << data1.isbn() << std::endl;
  6. return 0;
  7. }

? Codeblocks compilation Error

 
 
  1. error: passing ‘const Sales_data‘ as ‘this‘ argument of ‘std::string Sales_data::isbn()‘ discards qualifiers [-fpermissive]

If ISBN is a common function and this is a common pointer parameter, we should declare this as const sales_data * Const. After all, in the ISBN function, the object pointed to by this is not changed. Therefore, setting this as a pointer to a constant helps improve the flexibility of the function.

However, this is implicit and does not appear in the parameter list. Therefore, it is necessary to declare this as a pointer to a constant. In C ++, you can place the const keyword after the parameter list of the member function. In this case, the const following the parameter list indicates that this is a pointer to a constant. A member function using const is called a const member function ).

The ISBN function body can be imagined as follows:

 
 
  1. // Pseudocode that describes how the implicit this pointer is used
  2. // The following code is invalid: because we cannot explicitly define our own this pointer
  3. // Remember that this is a pointer to a constant, because ISBN is a constant member.
  4. std::string Sales_data::isbn(const Sales_data *const this)
  5. { return this->isbn; }

? Because this is a pointer to a constant, the constant member function cannot change the content of the object that calls it. In the preceding example, ISBN can read the data member of the object that calls it, but cannot write new values.

Note :? Constant objects and references or pointers of constant objects can only call constant member functions.



From Weizhi note (wiz)

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.