Const member functions in C + + (const after function declaration, or constant member function) usage explained

Source: Internet
Author: User
Tags class definition

In the C + + class definition, you can see a definition similar to the following:

class List {private:     * p_head;      int length;     ...... Public:     intconst;      BOOL Getnodeinfo (constintconst  {...}      BOOL Deletenode (constint  index);     ... ... ...}

As you can see, a const appears after the argument list of the two member functions of the GetLength and Getnodeinfo. This const indicates that the function does not modify the value of any member data of the class, called a constant member function.
For the external definition of the const function, it is also not possible to forget to write the Const qualifier, as defined below for the GetLength function (that is, the length of the returned list):

int Const // here still can't forget the const {     Return length;}

If any modification of object member data occurs in the definition of a const member function, it will be checked at compile time, such as:

int Const {     Return length// Error! }

The const member function exists in the sense that it can be called by a Const constant object. We all know that when defining an object or a variable, if you add a const to the type, such as a const int x, then the defined amount is a constant and its value cannot be modified. But the created object can call the member function, and the calling member function is most likely to change the value of the object, such as the following program:

Const List Mylist;mylist.deletenode (3// error, deletelength non-const member function

Obviously call Deletenode This member function deletes a linked list node, it is very likely to change the length of the object (linked list length) This value, which does not conform to the provisions of the Const object. However, it is very unreasonable to not allow a const object to call any member function. Therefore, we add the const specifier to the member functions that certainly do not modify the object's individual property values, so that at compile time, the compiler will check these const member functions, and if there is no behavior to modify the object values, the test passes. Later, if a Const constant object calls these const member functions, the compiler will allow it. Like what:

Const// Correct, getlength is the const constant function, which returns the chain table length, does not change // attribute value behavior, is tested by

You might ask, why not check for a const regular object when it calls the member function? If the called function changes the property value of the object, stop it immediately. This does not bother to add the Const qualifier to the member function. However, this will undoubtedly greatly increase compilation time. Consider this piece of code:

Const List MyList; Mylist.getlength (); Mylist.getlength ();

In this code, GetLength is called two times, but it is also checked two times at compile time, and if a member function is called multiple times, he will be checked at every call. This is obviously a big disadvantage. If the const qualifier is used to mark the constant function when defining the class, then the compiler checks only once, and the const function will be released directly when the const object calls the member function. Therefore, C + + takes the const qualifier to describe the usual function scheme and abandons the latter. Therefore, even if a function does not modify the behavior of an object's value, the Const object cannot invoke it if it does not add the const qualifier to the constant function.
However, there are times when we have to let the const function have the ability to modify a member's data value. Some internal state volumes, for example, do not matter to external users, but are useful for the entire object, such as caching-enabled technologies. In the case of this problem, we can define a member data as mutable (changeable), which means that this member variable can be modified by the const member function without breaking the law. For example, the following defines a Is_valid class member:

 class   list{ private  : ... mutable  bool       Is_valid; ...  public  :  bool  Checklist () const   { if  (length >= 1 ) then return           is_valid =true  ;  else  return  is_valid = false ; //  right!       };

This way, even a const member function like checklist modifies it to be legal.
However, when it is important to note that the Mutabe descriptor cannot be abused, it is appropriate to use mutable if only a few parts of a class are allowed to be modified by const constant functions. If most of the data is defined as mutable, it is best to put the data that needs to be modified in another separate object and access it indirectly.

Reference: "C + + programming Language Third Edition"
Note: This article is known for the original, reproduced please indicate the source!

Const member functions in C + + (const after function declaration, or constant member function) usage explained

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.