天天看點

關于C/C++ const變量 const指針 以及C++ 引用變量的解析

 關于C/C++ const變量 const指針 以及C++ 引用變量的解析

 首先我們知道const表示一個不能更改的值,在程式中往往使用這種屬性來保證安全,但是這種操作在C和C++中卻不同

 我測試中C++不能用MEMCPY進行更改但是C卻可以

 其中我們常見的

 const int a = 10;一個常量,不能更改其a的值

 const int *p1;一個指針但是他的傳回值是const int類型

 如我們可以

 p = &a;

 int* const p;一個指針,這個指針在整個生命周期中不能指向其他位置。

 const int* const p;一個指針,這個指針在整個生命周期中不能指向其他位置,并且傳回為const int類型

  我們在函數中也經常使用const

 const int* mul(const int *data,int step)

 const int *data:*data指向的值在函數中不能更改

 const int* mul:函數的傳回值為一個const int類型

 另外C++中包含一個引用變量概念

 如

 const int a = 10;

 const int & b =a;

 這裡&不是取位址,而是他是一個引用變量,b和a完全相同,不是拷貝,而是同樣的類型同樣的指針

 如果要用CONST描述就是

 const int* const b 和 &a相似

 b指針在生命周期中不能更改。也就是說int a=5; int &b=a;那麼b就不能在引用其他變量了。而指針不加int* const的情況下是可以的

但是如果int c=10; b=c; 那麼a和引用b都會變為10,也就是說引用是一個别名,對他的複制最終會影響他指向變量的值,因為他們的記憶體

區域是一塊。

!!引用隻能被初始化,并且必須初始化。

引用和指針差別

1、引用不能為空

2、new配置設定記憶體時隻能傳回給指針不能給引用

3、指針可以重新被指派改變指針的位址,引用必須初始化初始化後不能再次改變其引用的對象。

4、當函數傳回為按值後  主函數接收為引用則這個副本對象會被引用的生命周期更長然後析構,但是指針則是直接析構。

這一點過後講述

 下面是一個示範程式

  1 /*************************************************************************

  2   > File Name: quotevar.cpp

  3   > Author: gaopeng

  4   > Mail: [email protected] 

  5   > Created Time: Fri 27 May 2016 07:47:34 AM CST

  6  ************************************************************************/

  7 

  8 #include

  9 #include

 10 using namespace std;

 11 

 12 

 13 

 14 int main(void)

 15 {

 16     const int a = 10;

 17     const int & b =a;

 18     const int* const p = &a;

 19     const int *p1 = &a;

 20     cout << a <<endl;

 21     cout << b <<endl;

 22     cout << *p <<endl;

 23     cout << &a <<endl;

 24     cout << &b <<endl;

 25     cout << p  <<endl;

 26     cout << p1  <<endl;

 27 

 28     const int c = 20;

 29     p1 = &c; //true p1 is a nomarl pointer but *p1 is a const value

 30     //b = 30;    error a is const value b also is a const value

 31     //a = 30;    error a is const value

 32     //p = &c;    error p is a const pointer

 33     cout << &b  <<endl;

 34     cout << &c  <<endl;

 35     cout << p1  <<endl;

 36     // --part 2 memcpy also cant't change const int a's value

 37     int d = 10;

 38     int *e = &d;

 39     char j = 'A';

 40     void *p2;

 41     void *p3;

 42     const void *p4 = (const void *)(&j);

 43     p2 = (void *)(p);

 44     p3 = (void *)(e);

 45     if ( p2 == memcpy(p2,p4,1))

 46     {

 47         cout << "memcpy p2 is finsh!"<<endl;

 48     }

 49 

 50     if (p3 == memcpy(p3,p4,1))

 51     {

 52         cout << "memcpy p3 is finsh!"<<endl;

 53     }

 54 

 55     cout << a <<endl;

 56     cout << &a <<endl;

 57     cout << d <<endl;

 58     cout << &d <<endl;

 59     cout << p2 <<endl;

 60     cout << p3 <<endl;

 61 }  

 傳回值為

10

0x7ffd421b4ffc

0x7ffd421b5000

memcpy p2 is finsh!

memcpy p3 is finsh!

65

0x7ffd421b5004

使用的g++

可以看到完全的a和b有同樣值有同樣的位址,同時const 變量memcpy不能修改他的值,而普通的變量卻可以。

但是這個結論不适用于C

使用gcc

0x7ffc9a13a2bc

0x7ffc9a13a2c0

0x7ffc9a13a2c4

可以看到memcpy更改了const變量的值,這應該來說是不安全,申明const就是要說明不能更改。

c程式如下:

#include

int main(void)

{

        const int a = 10;

        const int* const b =&a;

        const int* const p = &a;

        const int *p1 = &a;

        printf("%d\n",a);

        printf("%d\n",*b);

        printf("%d\n",*p);

        printf("%p\n",&a);

        printf("%p\n",b);

        printf("%p\n",p);

        printf("%p\n",p1);

        const int c = 20;

        p1 = &c; //true p1 is a nomarl pointer but *p1 is a const value

        //b = 30;    error a is const value b also is a const value

        //a = 30;    error a is const value

        //p = &c;    error p is a const pointer

        printf("%p\n",&c);

        // --part 2 memcpy also cant't change const int a's value

        int d = 10;

        int *e = &d;

        char j = 'A';

        void *p2;

        void *p3;

        const void *p4 = (const void *)(&j);

        p2 = (void *)(p);

        p3 = (void *)(e);

        if ( p2 == memcpy(p2,p4,1))

        {

                printf("%s\n","memcpy p2 is finsh!");

        }

        if (p3 == memcpy(p3,p4,1))

                printf("%s\n","memcpy p3 is finsh!");

        printf("%d\n",d);

        printf("%p\n",&d);

        printf("%p\n",p2);

        printf("%p\n",p3);

}

</endl;

繼續閱讀