2013-03-10 44 views
-1

哪些更正確,更廣泛使用?真正的問題是最後一個,我認爲這種行爲會改變。關於聲明語義的幾個問題

int *test; //for this, it is probably both? 
int* test; 


int& test; 
int &test; 

實際問題:

const int test; 
int const test; 


const int* test; 
int* const test; //<-- I guess this also have a different meaning if I consider the last one? 


const int& test; 
int const& test; //also, considering last one, any difference? 


const int*& test; 
int* const& test; //<-- this is the correct one for stating "I won't modify the passed object, but I may modify the one pointed"? I have had problems with the other one sometimes, does the other one has some other meaning? 
const int* const& test; //<-- another meaning? 

此外,我會很高興,如果你能指出,如果你知道這個問題的任何視覺「ambiguations」

+1

有*是*裏面有一個有效的問題,如果你喜歡閱讀。如果你不知道答案,停止提議關閉。或者如果你不在乎,*** ***不在乎。 – Etherealone 2013-03-10 21:15:07

+1

查看[bjarne的常見問題](http://www.stroustrup.com/bs_faq2.html#whitespace)指針之一。 **編輯**:[他也談論const的位置](http://www.stroustrup.com/bs_faq2.html#constplacement)。 – 2013-03-10 21:16:29

+0

@JesseGood是的,我一直在閱讀。沒有與參考強硬的組合。我認爲這一切都是由指針引起的。雖然我會等待答案。 – Etherealone 2013-03-10 21:23:14

回答

1

如果您將空間放置在&*(或實際上如果您有一個或多個空白一側或兩側),則完全沒有區別。 const的位置確實有所作爲;

const int *test; 

意味着什麼test指向什麼沒有被改變。所以:

int b = 42; 
*test = 42; 
test = &b; 

*test = 42;將是非法的,但到了新的地址分配的測試是有效的。

int * const test; 

意味着test不改變不了它的價值,但它指向可以:

int b = 42; 
*test = 42; 
test = &b; 

現在test = &b;是無效的。

const int& test; 
int const& test; //also, considering last one, any difference? 

兩者相同。 const和int是&的同一側。

這一個:

const int*& test; 

意味着我們必須一個int *其中值不能被改變的參考。完全有效的,我們可以使用以下命令:

test = &b; 

兩個:

int* const& test 
const int* const& test; 

是一個int *const int *參考分別,我們不能改變指針本身 - 在所以沒有點通過引用傳遞它。

2

的你的例子給所有相同的語義的每一行,除了這些:

//test is a pointer to a const int. 
//test may be modified, but the int 
//that it points to may not (through this 
//access path). 
const int* test; 

//test is a const pointer to int. 
//test may not be modified, but the 
//int that it points to may. 
int* const test; 



//test is a reference to a pointer to a const int. 
//The referenced pointer may be modified, but 
//the int that that pointer points to may not be. 
const int*& test; 
//test is a reference to a const pointer to 
//int. The referenced pointer may not be modified 
//but the int may be. 
int* const& test; //<-- this is the correct one for stating 
        // "I won't modify the passed object, 
        //  but I may modify the one pointed"? 
        //  Yes 
        // I have had problems with the other one sometimes, 
        // does the other one has some other meaning? 
        //  Yes 
//test is a reference to a const pointer to const int. 
//The referenced pointer may not be modified, nor may 
//the int that it points to. 
const int* const& test; //<-- another meaning? 
1

首先,空格無關緊要技術上,除了它分隔符號的程度。

也就是說,通過分析現有的聲明,你不會試圖去理解事物。

您應該從開始構建聲明。

而在這些結構中,將const放在任何適用之後。

不幸的是,C語言的一般想法保存在C++中,聲明就像用法一樣。因此,如果例如*p這個表達式產生一個int,那麼p的聲明將是int *p。現在讓我們說,相反,表達式*p應該產生一個int const,那麼聲明將是int const *p

在C++中的重點是類型,因此一個C++程序員可能會寫,隨着

int const* p; 

無論從任何聲明的名稱分離式的東西。

但請記住,空間並不重要在技術上

通過這種從預期用法的角度構造聲明的方式,您可以輕鬆使用C++編譯器來測試您的聲明是否可以編譯。

+0

這是否意味着p是一個指向const int的指針,指向的對象可能不會被修改?它不同於「int * const p」的權利? – Etherealone 2013-03-10 21:30:06

+0

@Tolga:對,在C中, – 2013-03-10 21:31:06

0

int *xint &x被認爲是更好的。爲什麼? int* x, y看起來像是兩個指向int的指針,但是這是一個指針和一個int。 int *x, *y語法更好一些 - 你可以更好地看到這些是兩個指針。

我知道我沒有涵蓋你的問題的第二部分;)

+0

,肯定的。在C++中,你的首選約定不能被普遍應用,因此給出了不同外觀聲明的大雜燴。相反,在C++中,一次只聲明一件事。 – 2013-03-10 21:26:58

+0

從C開始有變化嗎? – cubuspl42 2013-03-10 21:27:51