Difference between const before and after a function

Source: Internet
Author: User
  • Difference between const before and after a function
  • 1. Const Basics

     
    If the const keyword does not involve pointers, we can understand it very well. The following is a case involving pointers:

    Int B =
    500;
    Const int * A = & B; [1]
     
    Int const * A = & B; [2]
    Int * const
    A = & B; [3]
    Const int * const A =
    & B; [4]

     
    If you can differentiate the above four situations, congratulations! you have taken a great step. I don't know. It doesn't matter. We can refer to "negative tive"
    In C ++, item21, if the const is on the left side of the asterisk, the const is used to modify the variable pointed to by the pointer, that is, the pointer is a constant; if the const is on the asterisk
    On the right side, const is to modify the pointer itself, that is, the pointer itself is a constant. Therefore, [1] is the same as [2], and the content pointed to by the pointer is a constant. In this case, you cannot change the content.
    For example, * A = 3
    ; [3] indicates that the pointer itself is a constant, and the content pointed to by the pointer is not a constant. In this case, the pointer itself cannot be changed, for example, a ++ is wrong; [4] indicates that the pointer itself and the content pointed to are both regular
    Quantity.
    In addition, some powerful functions of const are their application in function declaration. In a function declaration, const
    You can modify the return value of a function or a parameter. For a member function, you can also modify the entire function. In the following situations, we will gradually describe the usage: &
    Operator = (const A & );
    Void fun0 (const A * );

    Void fun1 () const; // fun1 () is a class member function.
     
    Const A fun2 ();

    2. Const Initialization

     
    Let's take a look at the initialization of the const variable.
    1) Non-pointer const constant initialization: a B;
    Const
    A A = B;

    2) pointer (reference) const constant initialization: A * D =
    New ();
    Const A * c = D;
    Or: const A * C
    = New ();
    Reference:
    A f;
    Const &
    E = f; // in this way, e can only access functions declared as const, but not General member functions;

     
    [Thinking 1]: Is the following assignment method correct?
    Const A * c = new ();
    A *
    E = C;
    [Think 2]: Is the following assignment method correct?
    A * const c =
    New ();
    A * B = C;

    3.
    The const modifier used as the parameter and return value.

     
    In fact, the principle is the same for both parameters and returned values. The const variable is initialized when a parameter is passed in and when the function returns.
    1
    Modify the const of a parameter, such as void fun0 (const A * A) and void fun1 (const
    A & );
     
    When you call a function, use the corresponding variable to initialize the const constant. Then, in the function body, perform the constant operation according to the Section modified by the const. For example, the parameter is const *
    A, you cannot change the content of the passed pointer to protect the content pointed to by the original pointer. For example, the parameter is const &
    A, the passed referenced object cannot be changed to protect the attributes of the original object.
    [Note]: The const parameter is usually used when the parameter is a pointer or reference;
     
    2. Modify the const of the returned value, such as const A fun2 (); const A * fun3 ();
     
    After the return value is declared, const is modified according to the "Modification Principle" to provide corresponding protection. Const rational
    Operator * (const rational & LHS, const rational & RHs)
     
    {
    Return rational (LHS. numerator () * RHS. numerator (),
     
    LHS. denominator () * RHS. denominator ());
    }

     
    The return value can be modified with const to prevent such operations from being allowed: Rational A, B;
    Radional C;
     
    (A * B) = C;

     
    The const is usually used to modify the returned value to the object itself when the binary operator reloads the function and generates a new object.
    [Summary]
    Generally, when the return value of a function is an object, if it is declared as const, it is mostly used for operator overloading. In general, it is not recommended to use const to modify the type of the function's return value to an object or
    When an object is referenced.
    The reason is as follows:
    If the returned value is const for an object or the reference of an object is const
    If the returned value has the const attribute, only the public data member and the const member function in Class A can be returned for the returned instance, and the value assignment operation is not allowed, which is rarely used in general.
    To.

    [Think 3]: Can I define a value assignment operator to overload a function?
    Const &
    Operator = (const A & );

    Use of const in class 4 member functions


    Generally placed behind the function body, such as void fun () const;
     
    If a member function does not modify the data member, it is best to declare it as const, because the const member function cannot modify the data member. If it is modified, the compiler reports an error.
    Greatly improve the robustness of the program.
    5. Const suggestions

    1
    To use const boldly, this will bring you endless benefits, but the premise is that you must understand the original principles;
    2
    Avoid the most common assignment errors. For example, assign values to the const variable;
    3
    When using const in parameters, you should use references or pointers instead of common object instances, for the same reason as above;
    4
    The three usage of const in member functions should be well used;
    5. Do not set the function return value type to const easily;
    6
    Except for overload operators, do not set the return value type as a const reference to an object;

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.