const pointer pointers to const

Reprinted from: http://www.cnblogs.com/lihuidashen/p/4378884.html

In the recent review of C ++, this is really heavy and difficult pointer, for a long time did not go ignored, a good sum up tonight const pointer, I have not written a blog, and record it -

const pointer defined:

  const pointer value of the pointer is a variable initialized, will not be able to change the point, the initialization is necessary. Form defined as follows:

type * const pointer name;

  When declaring pointer const keyword may be used before or after the type, it may also be used in both two positions. For example, the following statement is legal, but the big different meanings:

const int * pOne; // points to a constant integer  pointer, which points to the value can not be modified

int * const pTwo; // directed shaping constant pointer  , it can not point to another variable, but point to (variable) can be modified. 

const int * const pThree; // points to a constant integer  of constant pointer  . It can not point to anything else constant, pointing to the value can not be modified.

The trick is to understand these statements, see the const keyword to the right to determine what is declared as a constant, if the keyword is the right type, the value is constant; if the right keyword is a pointer variable, the pointer itself is constant. The following code will help illustrate this point:

const int *p1;  //the int pointed to is constant

int * const p2; // p2 is constant, it can't point to anything else

 

const pointers and const member functions

Keywords can be used for member functions. E.g:

Copy the code

class Rectangle

{

     pubilc:

        .....

        void SetLength(int length){itslength = length;}

        int GetLength () const {return itslength;} // constant member function declarations

        .....

     private:

        int itslength;

        int itswidth;

};

Copy the code

When a member function is declared as const, if you try to modify the data object, the compiler will be considered an error.

If you declare a pointer to a const object, then call the const pointer by the method (member function) only.

Example declares three different Rectangle objects:

Copy the code

Rectangle* pRect = new Rectangle;

const Rectangle * pConstRect = new Rectangle; // const object point

Rectangle* const pConstPtr = new Rectangle;

// pConstRect is a pointer to const pointer to an object, it can only be declared as const member functions, such as GetLength ().

Copy the code

 

C onst pointers and pointers to const

When used with a const pointer actually has two meanings. A pointing is that you can not modify the contents of the pointer itself, and the other refers to is that you can not modify the content pointer. It sounds a little confusing for a while put up an example to understand.

      Let me talk a pointer to a const, it means a pointer to the content can not be modified. It has two usages.

      const int * p; (recommended)

      int const * P;

      first to be understood, p is a pointer to the content type is const int. p itself without initializing it can point to any identifiers, but it points to the content can not be changed.

      The second can easily be understood to be a p int const pointer (pointer itself can not be modified), but such understanding is wrong, it is also represented by the pointers to const (pointer to the content can not be modified a) it is an expression of the first with a meaning. To avoid confusion recommend you use first.

      Moreover const pointer, it means that the value of the pointer itself can not be modified. It is only one of writing

      int * const p = address; (because the value of the pointer itself can not be modified so that it has to be initialized)

      which may be understood to form, p is a pointer that points to a const int pointer. It points to a value that can be altered as * p = 3;

      there is a case where the pointer is pointing to itself and its contents are not being changed, please read.

      const int * const p = address;

      int const * P = const an address;

      Read the contents of the above is a bit dizzy, it does not matter, you do not have to back it, with more to know, there is a technique by observing the above rule is not difficult to sum up a little, what is it? The rule is: a pointer to a const (pointer to content can not be modified) const key word is always left in the emergence * const pointer (the pointer itself can not be modified) const key word is always in the right * appears, it goes without saying that the middle two const * certainly add a pointer pointing to itself and its contents are not being changed. With this rule in mind is not like much.

Copy the code

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 #include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int a=3;
    int b;
    
    / * Define pointers to const (pointer to the content can not be modified) * / 
    const int* p1; 
    int const* p2; 
    
    / * Define const pointer (pointer value itself since it can not be changed have to initialize) * / 
    int* const p3=&a; 
    
    / * Pointer points to itself and its contents are not therefore have to be changed initialization * /
    const int* const p4=&a;
    int const* const p5=&b; 
    
     p1 = p2 = & a; // correct
     * P1 = * p2 = 8; // incorrect (pointer to the content can not be modified)
    
     * P3 = 5; // correct
     p3 = p1; // incorrect (the pointer value itself is not changed) 
    
     p4 = p5; // incorrect (the pointer pointing to itself and its contents are not to be changed) 
     * P4 = * p5 = 4; // incorrect (the pointer pointing to itself and its contents are not to be changed) 
     
    return 0; 
}

Copy the code

 

const Usage Summary:
const most common is to define constants, in addition, it can be modified function parameters, return values, and the definition of body functions.
1. const modified function parameters
if the parameter for output, regardless of what type of data it is, and whether it is a "pointer is passed" or "passing by reference" can not add the const modifier, otherwise parameter will lose output.
const only modifies the input parameters:
If the input parameter using the "pointer is passed", then const qualifier prevents unintentional alteration of the pointer, play a protective role.
The "const &" usage modified input parameters are summarized as follows:
(1) input parameter for a non-intra data type should be 'pass the value "mode to the" const reference Delivery ", aims to improve the efficiency. For example, void Func (A a) to void Func (const A & a) .
(2) For input parameters of the internal data type, not the "value pass" mode to the "const reference to pass." Otherwise, not only reach the purpose of improving efficiency, but also reduce the intelligibility of the function. E.g. void Func (int x) should not be changed void Func (const int & x) .


2. const modifier function return value
if the given "pointer is passed" mode function returns the value const qualifier, then the function returns the value (i.e. pointer) content can not be modified, only the return value is assigned to the const qualifier the same type of pointer. Such as the function
const char * GetString (void);
following statement compilation errors:
char * STR = the GetString ();
correct usage is
const char * str = GetString () ;

if the return value is not an internal data type, the function A GetA (void) rewriting const A & getA (void) can indeed improve the efficiency. But this time must be careful, we must find out whether the function is to return an object of "copy" or return only the "Alias" on it, otherwise the process will go wrong.
Using the function returns the value "transmitted reference" not many occasions, only in this way is generally a function of class assignment, intended to fulfill chain expression.
E.g:

Copy the code

class A
{
A & operate = (const A & other); // mutator
};
A a, b, c; // a, b, c of object A
a = b = c; // normal chain assignment
(A = b) = c; // assignment abnormal chain, but legitimate

Copy the code

If you assign a function's return value const qualifier, then the return value of the content can not be edited. In the embodiment, a = b = c statement is still correct, but the statements (a = b) = c is illegal.


3. const member functions modified
several rules on Const function:
.. A const object can be accessed only const member functions, rather than const object can access any member function, including const member functions
. B const member object can not be modified , but const pointer to an object by object of maintaining it can be modified.
c. const member functions can not modify the data object, regardless of whether const object has properties. it is at compile time, whether to modify member data as the basis for examination .
D. However mutable data members plus modifiers, for any event can be modified by any means, in this case natural const member functions can modify its

Guess you like

Origin blog.csdn.net/alss1923/article/details/87918641