Const class member functions for C + +

Source: Internet
Author: User

http://blog.csdn.net/lihao21/article/details/8634876

We know that in C + +, if a variable is declared as a const type, the operation that attempts to modify the value of the variable is considered a compilation error. For example

[CPP]View PlainCopy 
    1. Const Char blank = ';
    2. blank = ' \ n '; //Error

In object-oriented programming, in order to embody encapsulation, the data members of the class object are not usually allowed to be modified directly. To modify a class object, you should call the public member function to complete. To guarantee the Const object's constant nature, the compiler must distinguish between unsafe and secure member functions (that is, a function that attempts to modify a class object and not modify a class object). For example

[CPP]View PlainCopy 
    1. const screen Blankscreen;
    2. Blankscreen.display (); //Object read Operation
    3. Blankscreen.set (' * '); //Error: Const class object does not allow modification

In C + +, only member functions declared as const can be called by a Const class object.

To declare a class member function of a const type, simply add the keyword const after the member function argument list, for example,

[CPP]View PlainCopy 
    1. Class Screen {
    2. Public
    3. Char get () const;
    4. };

When you define a const member function outside the class body, you must also add the const keyword, such as

[CPP]View PlainCopy 
    1. Char screen::get () const {
    2. return _screen[_cursor];
    3. }

If you declare a member member function as const, the function does not allow you to modify the data members of the class. For example

[CPP]View PlainCopy 
    1. Class Screen {
    2. Public
    3. int OK () const {return _cursor;}
    4. int error (INTIVAL) const {_cursor = ival;}
    5. };

In the definition of the member function above, OK () is defined as valid, and the definition of error () is illegal.

It is important to note that declaring a member function as const guarantees that the member function does not modify the data member, but if the member is a pointer, the const member function does not guarantee that the object pointed to by the pointer is not modified and the compiler does not detect the modification as an error. For example

[CPP]View PlainCopy 
  1. Class Name {
  2. Public
  3. void SetName (const string &s) const;
  4. Private
  5. Char *m_sname;
  6. };
  7. void SetName (const string &s) Const {
  8. M_sname = S.c_str (); //Error!  Cannot modify m_sname;
  9. for (int i = 0; i < s.size (); ++i)
  10. M_sname[i] = s[i]; //bad style, but not wrong
  11. }

Although M_name cannot be modified, but M_sname is a char * type, the const member function can modify the character it points to.

A const member function can be overloaded with a non-const member function that has the same argument list, for example,

[CPP]View PlainCopy 
    1. Class Screen {
    2. Public
    3. Char get (int x,int y);
    4. Char get (int x,int y) const;
    5. };

In this case, the constant nature of the class object determines which function is called.

[CPP]View PlainCopy  
    1. Const screen CS;
    2. Screen CC2;
    3. char ch = cs.get (0, 0); //Call the const member function
    4. ch = cs2.get (0, 0); //Call non-const member function

Summary:

1) The const member function can access non-const data members of non-const objects, const data members, or access all data members within a const object;

2) Non-const member functions can access non-const data members of non-const objects, const data members, but cannot access arbitrary data members of Const objects;

3) as a good programming style, when declaring a member function, if the member function does not modify the data member, the member function should be declared as a const member function whenever possible.

Const class member functions for C + +

Related Article

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.