Talk about const

Source: Internet
Author: User
Tags modifier
Const member functions (constant membership function)

Tells the compiler that the member function cannot change the member variable of the class

Project Const Object (constant objects data members cannot be changed) Non-const Object (the data members of the extraordinary measure can be changed)
Const member functions (guaranteed not to change the data members) Yes Yes
Non-const member functions (does not guarantee that the data members are unchanged) No Yes

When the const and NON-CONST versions of a member function exist at the same time, the Const object can only invoke the const version, Non-const object can only invoke the Non-const version;

Class template std::basic_string<...> has the following two member functions:
CharT operator[] (size_type pos) const {/No need to consider Copy on Write/}
Reference operator[] (size_type POS) {/must consider Copy on Write/}

String uses the Count mode, so a variable of the same content is shared. The two functions above may be taken to modify the data, so it is necessary to do copy on Write, that is, if the contents of the STR1,STR2,STR3 are "AAAAA", then three of them will share "aaaaaa", but one of the strings is to modify the content, STR1 to modify the second character in the string to ' B ', a copy of "AAAAAA" is copied first, and then "ABAAAA" is modified. At this time str2 and STR3 will still share "aaaaaa", but STR1 will not, its value is "ABAAAA".

Look at one of the following examples:

Code 1

Class Test {public
:
    void Fun (int i) {cout << "I:" << i << "Fun non-const" << Endl;}
    //void Fun (int i) const {cout << "I:" << i << "Fun const" << Endl;}
;

int main (int argc, char *argv[]) {
    test test;
    Test.fun (1);
    return 0;
}

Output result:
fun Non-const

Code 2

Class Test {public
:
    //void Fun (int i) {cout << "I:" << i << "Fun non-const" << Endl; }
    void Fun (int i) const {cout << "I:" << i << "Fun const" << Endl;}
;

int main (int argc, char *argv[]) {
    test test;
    Test.fun (1);
    return 0;
}

Output result:
i:1 Fun const

The above two code tests show that a non-const object can call either a constant member function or a very member function. Then if these two functions exist at the same time, they will constitute an overload. If an overload is made, a non-const object will invoke that one. Look at the code below ...

Code 3

Class Test {public
:
    void Fun (int i) {cout << "I:" << i << "Fun non-const" << Endl; }
    void Fun (int i) const {cout << "I:" << i << "Fun const" << Endl;}
;

int main (int argc, char *argv[]) {
    test test;
    Test.fun (1);
    return 0;
}

Output result:
i:1 fun Non-const

Code 3 compiles smoothly and runs the output i:1 fun non-const, stating that when a constant member function and a very high member function exist simultaneously, the very object invokes the very measure member function.

Code 4

Class Test {public
:
    void Fun (int i) {cout << "I:" << i << "Fun non-const" << Endl;}
    //void Fun (int i) const {cout << "I:" << i << "Fun const" << Endl;}
;

int main (int argc, char *argv[]) {
    const Test consttest;
    Consttest.fun (2);
    return 0;
}


Compilation error:
error:member function ' fun ' not viable: ' This ' argument has type ' const Test ', but function isn't marked cons T
        consttest.fun (2);

A constant object invokes a very member function, which is not compiled.

Code 5

Class Test {public
:
    //void Fun (int i) {cout << "I:" << i << "Fun non-const" << Endl; }
    void Fun (int i) const {cout << "I:" << i << "Fun const" << Endl;}
;

int main (int argc, char *argv[]) {
    const Test consttest;
    Consttest.fun (2);
    return 0;
}

Output result:
i:2 Fun const

Constant object calls a constant member function, no problem. But if: the constant member function and the extraordinary member function all exist, the constant object will call that.

Code 6

Class Test {public
:
    void Fun (int i) {cout << "I:" << i << "Fun non-const" << Endl;}
    void Fun (int i) const {cout << "I:" << i << "Fun const" << Endl;}
;

int main (int argc, char *argv[]) {
    const Test consttest;
    Consttest.fun (2);
    return 0;
}

Output result:
i:2 Fun const

Constant member functions and very high volume member functions exist, the constant object calls the constant member function.

Code 7

Class Test {public
:
    void Fun (int i) {cout << "I:" << i << "Fun non-const" << Endl; }
    void Fun (int i) const {cout << "I:" << i << "Fun const" << Endl;}
;

int main (int argc, char *argv[]) {
    test test;
    Test.fun (1);

    Const Test Consttest;
    Consttest.fun (2);
    return 0;
}

Output result:
i:1 fun non-const
i:2 Fun const

When both a constant member function and a non-permanent member function exist, the very object will invoke the very magnitude member function, and the constant object will invoke the constant member function.

Summarize:
1. Const modified member functions, not only restrict the function to modify the member variable, but also can implement the function of overloading
2. The member function of the Const modifier, if not overloaded, both the non-const object and the const object can be invoked;
3. The member function of the Const modifier, if overloaded, a non-const object can only invoke a member function that is not modified by const, and the const object can only invoke a member function that is modified by a const.

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.