A detailed description of C + + const usage

Source: Internet
Author: User
Tags modifier

1.const Constants and Macro constants
const int max=100;
#define MAX 100;
The const constant relative to the macro constant has two a bit:
1) have type security check
2) Less memory footprint

Position of 2.const variable in memory
We have explained the example in the memory layout of the C program.

#include <stdio.h>Const intA =Ten;////All Constants aintMain () {Const intb = -;//Local constant b    int* PA = (int*) &a;//error, because all constants are placed in read-only data segments    int* PB = (int*) &b;//modification succeeds because local constants are placed on the stack*PA = -; *PB = -; return 0;}

Initialization of 3.const constant member variables
IS constant for the lifetime of an object.

class test{public:    constint  A;    Test (int  i): A (i) {}}

Const constants can only be initialized by initializing the list .

4.const decorated objects
The object cannot be modified. Only member variables can be accessed and cannot be modified. You can only invoke the constant member function .
About the constant member function, which we introduce later.
As shown in the following example:

#include <iostream>classtest{ Public:    intA; voidfun0 () {}voidFUN1 ()Const{}};intMain () {test test; Consttest& a_test =test; Std::cout<<a_test.a<<std::endl;//Normal, Access public member aA_test.fun0 ();//error, object contains type qualifier incompatible with member functionA_test.fun1 ();//Normal    return 0;}

5.const modifier pointer
Let's look at the following four ways of writing:

1 Const int Ten ; 2 int Const Ten ; 3 int Const Ten ; 4 Const int Const ten;

What we need to know is that
Const on the left, indicating that the pointer points to a constant
Const in * Right, indicating that the pointer itself is a constant
Therefore, 1), 2) indicates that the content pointed to by pointer A cannot be modified
3) indicates that pointer a cannot be modified by itself
4) indicates that the contents of pointers A and a are not modifiable

6.const modifier member function, function parameter, return value
1) Modifier member function

void Const;

The member function name is followed by a const, which indicates that the function is a constant member function.
The const implicit modification of the constant member function is the this pointer, that is, the object is non-modifiable,
Therefore, the constant member function can only access member variables and cannot be modified. Only other regular member functions can be called .
As shown in the following example:

intg_a;voidG_fun () {}classtest{ Public:    voidfun0 () {}voidFUN1 ()Const{}     voidFun2 ()Const{a=Ten;//error, "must be modifiable left value"Fun0 ();//error, object contains type qualifier incompatible with member functionFun1 ();//Normal, other regular member functions can be calledg_a=Ten;//NormalG_fun ();//Normal    }Private:    intA;}

2) Modifier function parameters

void fun0 (const test* p_test); void fun1 (const test& Test);

Before the passed-in parameters, add the const, indicating that the parameters passed in cannot be modified.

3) modifier function return value

Const Test fun0 (); Const test* fun1 ();

When using the const test* FUN1 (); When a pointer is returned, the return value can only be assigned to a const-decorated pointer of the same type.
As shown in the following example:

classtest{ Public:    voidfun0 () {}ConstTest fun1 () {return* This; }    Consttest*fun2 () {return  This; }};intMain () {test test; Test R=test.fun1 (); Consttest* RP =test.fun2 ();}

A detailed description of C + + const usage

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.