2011-03-11 77 views
5
#include <iostream> 
using namespace std; 

int main(int argc, char* argv[]) 
{ 
    int i1 = 0; 
    int i2 = 10; 

    const int *p = &i1; 
    int const *p2 = &i1; 
    const int const *p3 = &i1; 

    p = &i2; 
    p2 = &i2; 
    p3 = &i2; 

    cout << *p << endl 
     << *p2 <<endl 
     << *p3 <<endl; 
    return 0; 
} 

該代碼可以用VC6.0和VC2010編譯。 但我有疑問的打擊:關於「int const * p」和「const int * p」

const int的* P = &i1;

這意味着什麼, 「P」 點不能被修改,但不能通過p修改,是嗎? 所以

P = &i2;

這條線可編譯,是嗎?

這條線:

int const *p2 = &i1; 

在我看來,這意味着什麼,而P2點是可以改變的,我說的對P2不能被修改? 爲什麼

P2 = &i2;

可以編譯?

關於這一行:

const int的常量* P3 = &i1;

P3 = &i2;

哦,天哪......我瘋了。我不知道爲什麼這行可以編譯沒有錯誤... 任何機構可以幫助我嗎?

這讓我感到困惑的另一個代碼是在這裏:

class Coo2  
{  
public:  

Coo2() : p(new int(0)) {}  

~Coo2() {delete p;}  


    int const * getP() const 
    {  
     *p = 1;   
     return this->p;  
    }  

private:  
     int* p;  
}; 

爲什麼這個代碼可以編譯? 在

INT常量* getP()const的

我更改數值或* P!

+0

'const'適用於左邊的東西,除非左邊沒有東西,在這種情況下它適用於右邊的東西。 (是的,這是不必要的混淆和愚蠢。) – GManNickG 2011-03-11 03:07:23

+0

'const'在最後意味着不會對對象/類進行更改。 – 2011-03-11 03:21:07

回答

5

隨着指針的幫助下,你其實可以做兩件事情。

  1. 您可以更改它指向的數據,但不能指向不同的內存位置。
  2. 您可以將其指向不同的內存位置,但不能更改它指向的數據。

現在,當你說,int const * ptr或者int const * ptr時,它就屬於第一類。這是一樣的 -

const int num = 5; // Both mean the same. 
int const num = 5; 

要,居然沒有能夠改變指針到不同的位置,即,以一個恆定的位置,但可以修改數據,語義應該是int* const。由於指針的內容是一個常量,它應該在聲明時進行初始化。

int num = 5; 

int* const ptr; // Wrong 
ptr = &num; // Wrong 

int* const ptr = &num; 
*ptr = 100; 

但是,還有第三種。指向常量位置的常量指針,既不能指向不同的內存位置,也不能更改指向的數據。 (即,const int * const)

而現在回答問題,前兩個可以編譯,因爲它們沒有指向常量位置。所以,他們也可以在後期進行修改。

const int const *p3 = &i1; 
p3 = &i2; // Wrong 

在上面的代碼中,p3是一個常量指針到恆定位置。所以,它不能被修改。

const在成員函數結束時表示它不會改變對象的狀態。當你說*p = 1;時,你並沒有改變對象的狀態。 p仍然指向相同的內存位置。這是不允許做的事 -

int const * Coo2::getP() const 
{  
    *p = 1; // State of `p` is still not modified. 
    p = new int ; // Error: Changing the memory location to which p points. 
        //  This is what changing the state of object mean and  
        //  is not allowed because of `const` keyword at the end of function 
    return this->p;  
} 

希望,現在你明白爲什麼程序編譯:)

1

不,const關鍵字之前的*表示您指向的變量是「const」變量,只有它不能被修改。

  1. 如果你想能不能重新分配,那麼你需要將其聲明爲Foo* const p = &bar;
  2. 指針如果你想指向不能被重新分配一個「常量」對象的指針聲明爲const Foo* const p = &bar

這是完全沒有以具有const int* foo指針被分配給的const int* const bar指針就像它是細到具有分配給一個const int一個int的值。以同樣的方式思考它。

1

INT常量*相同const int的*

4

int const * p;const int * p是相同的。它是當const之後*那 表達式的語義變化。

我知道,這很瘋狂。

+0

int const * getP()const',最後一個'const'意味着我無法改變* p('p'指向什麼)? – 2011-03-11 03:10:39

+0

好吧,你對細節的關注正在逐漸成爲學習語言的途徑(這不是一件壞事)。當const出現在函數簽名之後時,它意味着成員函數(coloquily,定義爲屬於某個對象的函數)不打算改變對象。編譯器在一定程度上強化了這種意圖。因此,如果所述函數寫入或調用可寫入對象的函數,或者以任何其他間接(但可由編譯器檢測到)的方式編譯器將引發錯誤。 – 2011-03-11 03:18:35

+0

@Bug:不,這意味着成員函數是'const'。你有[良好的入門書](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)? – GManNickG 2011-03-11 03:18:43

2
const int *p = &i1; 
int const *p2 = &i1; 

這些都聲明non-const指向const數據。

也就是說,使用p,您不能更改它指向的數據。但是,您可以更改指針本身,例如,通過指定爲合法的p = &i2。但*p = 87987是非法的,因爲數據p指向的是const!

-

int * const p = &i1; 

這聲明常量指針到非const數據。也就是說,p=&i2是非法的,但*p = 98789是合法的。

-

const int * const p = &i1; 

這聲明常量指針爲const數據。也就是說,現在p=&i2*p=87897都是非法的。

2

兩者完全相同。重要的是相對於星號(*)限定符的位置:

int const *p; // normal pointer to const int 
const int *p; // ditto 

int *const p; // const pointer to normal int (rarely useful) 

int const * const p; // const pointer to const int 
+0

第二個代碼段:int const * getP()const,最後的constmeans是否不能改變* p(p指向什麼)? – 2011-03-11 03:18:26

+1

@BugCreater:否 - 它是成員函數的限定詞,表示該函數不會更改其對象的狀態,因此可以在const對象上調用它。 – 2011-03-11 03:20:07

7

這裏我們考慮4種類型的指針聲明:

  1. int * w; 這意味着, w是一個指向整數類型值的指針。我們可以修改指針及其內容。如果我們初始化w當聲明如下: int * w = &a;
    然後,由以下兩個操作是可行的:
    w = &b;(真)
    *w = 1;(真)

  2. int * const x;
    這意味着x是一個常量指針指向一個整型值。如果我們初始化x而聲明如下:
    int * const x = &a;
    然後,我們不能這樣做:x = &b;(wrong),因爲x是一個常量指針,不能修改。
    但是,可以這樣做:*x = 1;(true),因爲x的內容不是固定的。

  3. int const * y; //既意味着同樣
    const int * y;
    這意味着,y是一個指向恆定整數值的指針。如果我們初始化y,同時聲明如下:
    int const * y = &a;
    然後,可以這樣做:y=&b;(true)因爲y是一個非常量指針,可以指向任何地方。
    但是,我們不能這樣做:*y=1;(wrong),因爲y指向的變量是一個常量變量,無法修改。

  4. int const * const z; //既意味着同樣
    const int * const z;
    這意味着,z是一個常量指針指向一個恆定的整數值。如果我們初始化ž同時聲明如下:
    int const * const z = &a;
    因此,以下操作的不都是可行的:
    z = &b;(wrong)
    *z = 1;(wrong)

1

簡潔地;讀/寫int &指針的每個組合;

int main() { 

    int a,b; 

    int* w;      // read/write int, read/write pointer 
    w= &b;      // good 
    *w= 1;      // good 

    int* const x = &a;   // read only pointer, read/write int 
    // x = &b;     // compilation error 
    *x = 0;      // good 

    int const * y;    // read/write ptr, read only int 
    const int * y2;    // " " " 
    y = &a;      // good 
    // *y = 0;     // compilation error 
    y2 = &a;      // good 
    // *y2 = 0;     // compilation error 

    int const * const z = &a;  // read only ptr and read only int 
    const int * const z2 = &b; // " " " " 
    // *z = 0;     // compilation error 
    // z = &a;     // compilation error 
    // *z2 = 0;     // compilation error 
    // z2 = &a;     // compilation error 

} 
相關問題