C ++: const keyword

Source: Internet
Author: User

The const keyword does not represent a constant, but "the content cannot be modified ". The variable modified by const does not necessarily have the same value. It may be changed by external conditions that cannot be detected by the compiler. Const and volatile can simultaneously modify a variable.

Compared with # define, const is a common problem in the interview. # Define defines constants, which are replaced by the Preprocessor characters. The Compiler does not perform type security checks. Const defines a variable with a constant value, and the compiler checks its type security. In C ++, we recommend that you use const instead of # define macro to define constants.


// XK> scope of the const object

In C, the const object is externally connected by default, that is, the const object name is global. In C ++, the const object is connected internally by default, that is, the scope is only in the file that defines the const object.

// It is wrong to write it in C ++. The const object must be initialized during definition. // But it is correct in C. The C compiler only treats it as a declaration. Const int val1; // The statement in C ++ that the const object must be explicitly used with externextern const int val2; // in C ++, the default const is an internal connection. The initialization expression is defined rather than declared. extern is explicitly declared as an external connection extern const int val3 = 100;

The C compiler cannot regard a const object as a constant during compilation. While C ++ wants to replace # define with const, it will naturally try to treat the const object as a constant in the compilation period (the professional term is called a constant expression. Note that not all const objects are constants in the compilation period ). Therefore, it is wrong to write the following code in C, but it is common in C ++.

const int bufsize = 100;char buf[bufsize];


// XK> const pointer and pointer to const object


// XK> const reference


// XK> const modifier function parameters


// XK> const modifier function return value


// XK> const modifier member functions

After the const keyword is added to the form parameter table of the member function, it indicates that this is a const member and the data member of the object operated by the member function cannot be modified (except for the data member modified with the mutable keyword, the mutable keyword is used to modify a data member in the const member function ). For example:

Class myclass {public: Double myfunc (int A, int B) const ;}; doublemyclass: myfunc (int A, int B) const {// you can see from this, {of the function body is better than another line. Awkwardly following const}

Note: const must appear in the declaration and definition of member functions at the same time; otherwise, an error is reported during compilation.

// XK> const member variable


// XK> const and overload

If the function parameter is not a reference or pointer, the const version and the non-const version cannot constitute an overload.

// Compilation error: function redefinition. The parameter is not a reference or pointer and cannot be overloaded Based on the const overload int myfunc (int A) {} int myfunc (const int A) {} // PS: an overload can be formed in a single scope, not a member function

The reason is that the real parameter is passed. When initializing the form parameter with the real parameter, the system does not consider whether the form parameter is const. The function only controls the copy and cannot change the real parameter. There is no essential difference between passing a const real parameter to a const parameter or a non-const parameter.

If the function parameter is a reference or pointer, the function that points to the const object and the function that points to the non-const object can constitute an overload.

// Myfunc1 can constitute an overloaded int myfunc1 (const int * A) {} int myfunc1 (int * A) {} // myfunc2 cannot constitute an overload. Whether the parameter itself is const does not affect the real parameter int myfunc2 (int * const A) {} int myfunc2 (int * ){}

In addition, the const member function of the class and the non-const member function can also constitute an overload.

// Overload int myclass: myfunc () // This pointer type is myclass * const {...} int myclass: myfunc () const // This pointer type is const myclass * const {...}

In general, all member function modifiers such as static and virtual only need to use these keywords in the member function declaration in the class definition, but const is an exception, you must use the keyword const at the member function declaration in the class definition and the member function definition outside the class definition. The reason is that the const member function and the non-const member function constitute an overload, not a function.

// XK> the const cannot be

Const cannot be declared as Const. Const const constructor is unnecessary, even if you create a const object, because the const function is initialized.

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.