What are the limitations of constant member functions?

Source: Internet
Author: User
Code:
  1. Class coo
  2. {
  3. Public:
  4. Coo (): A (0)
  5. {}
  6. Int geta () const // constant member function
  7. {
  8. ++ A; // compilation Error
  9. Return this->;
  10. }
  11. PRIVATE:
  12. Int;
  13. };

Of course, this Code cannot be compiled. But what about the following section?

Code:
  1. Class CoO2
  2. {
  3. Public:
  4. CoO2 (): p (New int (0 )){}
  5. ~ CoO2 () {Delete P ;}
  6. Int const * getp () const // constant member function
  7. {
  8. * P = 1; // compile OK
  9. Return this-> P;
  10. }
  11. PRIVATE:
  12. Int * P;
  13. };

The answer is that the compilation is successful because the code is correct. In terms of semantics, CoO2 objects have pointers instead of pointers. :)

If you want to communicate with me, click the following link to become a friend:
Http://student.csdn.net/invite.php? U= 112600 & C = f635b3cf130f350c

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.