可能重複:
Is there a difference in C++ between copy initialization and direct initialization?兩個簡單的變量初始化的問題
是否有任何區別,
int x(5);
int x = 5;
&什麼之間的區別,
int const x(5);
const int x(5);
可能重複:
Is there a difference in C++ between copy initialization and direct initialization?兩個簡單的變量初始化的問題
是否有任何區別,
int x(5);
int x = 5;
&什麼之間的區別,
int const x(5);
const int x(5);
一個非常好的規則:
讀聲明從右到左。
(見Vandevoorde/Josutiss 「C++模板:完全指南」)
如:
int const x; // x is a constant int
const int x; // x is an int which is const
// easy. the rule becomes really useful in the following:
int const * const p; // p is const-pointer to const-int
int const &p; // p is a reference to const-int
int * const * p; // p is a pointer to const-pointer to int.
自從我遵循這個原則進行的拇指,我從來沒有曲解這種聲明再次。
(:sisab retcarahc-REP無噸,sisab nekot-REP無薄膜電致發光-OT-thgir NAEM我hguohT:?潮
!! dekicw ylpmis –
@iamcreasy:在評論中有一個錯字。 –
@ martinho-fernandes'從右至左閱讀.' –
除非您使用參數,否則兩者都不完全相同。這只是編碼風格的問題。
[注:但下面還是不一樣的:
int x; // x is variable
int x(); // x is function declaration
]
這是一個更好的答案,因爲它引發了一個有用的警告。 – Benoit
沒有區別。替代方法將變量初始化爲一個值。
但是你在你的系統上試過這個嗎?你得到了什麼?
我試過了,他們是一樣的。我認爲一些技術細節可能會潛伏在後面。 –
int x(5);
int x = 5;
沒有區別。
int const x(5);
const int x(5);
沒有區別。
int x = 5;
const int *p = &x;
int *const q = &x;
這裏是p
和q
的差異。使用p
您無法更改x
的值,但可以將新地址分配給p
。使用q
,您不能指向任何其他內存位置,但可以使用q
修改x
。
然而,有在C++中沒有差異,在C, - int x(5);
不允許
你顯示4行中,僅有1有效C.
int x(5); /* INVALID C */
int x = 5;
int const x(5); /* INVALID C */
const int x(5); /* INVALID C */
我不知道意思其他語言中的無效行。在C中,有效行的含義是創建名稱爲x
,類型爲int
,值爲5
的對象。
要定義一個「常量」(更好:一個「只讀」)對象,C語法是
const int x = 5;
int const x = 5;
但x
不能在僅常數允許的地方使用。x
不是一個常數:它的類型是const int
#define CONSTANT 42
enum /*unnamed*/ { First=1, Second, Third };
switch (var) {
case x: break; /* invalid use of x; only constants allowed in case labels */
case 15: break; /* ok; 15 is a constant */
case CONSTANT: break; /* ok; CONSTANT is a constant with value 42 */
case Second: break; /* ok; Second is a constant with value 2 */
}
是,'const int x = 5'在C中有效嗎? –
是的,它會創建一個名爲'x'的對象,其值爲'5',限定類型爲'const int'。注意(非限定或基本)類型仍然是'int';它不會創建一個常量(例如,在案例標籤中可用):它會創建一個無法通過該標識符進行更改的對象。 – pmg
使用'case x:break;'我必須使用'const int x = 5;'? –
的對象關於const
關鍵字:
所以,int const i
和const int i
是相同的。
但是,int *const i
和const int *i
是不一樣的:在第一個表達式中,指針是常量,在第二個表達式中,指向的整數是常量。 Als int const* i
相當於第二個。
*性格,哪一方首先工作?左還是右? –
'*'或者遵循**類型名稱**:在這種情況下,它是一個類型修飾符(將類型修改爲指針類型),或者遵循**表達式**:在這種情況下,它是乘法運算符,或者在綁定到指針類型的表達式之前:它是** dereference **運算符。沒有這樣的規則。 – Benoit
int * const i':我是一個const指針,它指向一個非const int&'int const * i':我是一個指向const int的指針。它是正確的? –
首先,內置類型沒有區別。對於班級類型,T x = y
表示法需要一個拷貝構造函數,T x(y)
表示法不需要。 編輯:另外,正如其中一位評論者指出的,如果構造函數T(y)
被聲明爲explicit
,則T x = y
將不起作用。
對於第二,有沒有什麼區別,只要編譯器而言,但第二(const int
)會導致混亂在許多情況下,尤其是當typedef
s的參與,如:
typedef int* IntPtr;
IntPtr const cpi1; // Const pointer to a non-const int.
const IntPtr cpi2; // Also a const pointer to a non-const int.
在兩個定義,const
適用於指針。
由於這種混淆,通常認爲更好的做法是將const
放在修改後的位置。現在—很長一段時間,把const
放在一開始是平常的,結果是這種做法很普遍。關於const
拇指
'const'在開始時也是通常用標準寫的。 –
「對於類類型,」T x = y「表示法需要一個拷貝構造函數,」T x(y)「表示法不需要。」事實並非如此。兩者都需要一個拷貝構造函數。 'T x = y'看起來像是任務,但不是一個。 –
@Fernandes是的,標準是基於ARM的。它似乎沒有什麼值得麻煩改變(還有一些老的程序員習慣於「const int」,如果我們改變了,他們會覺得不舒服)。 「普遍接受」的最佳實踐已經改變爲支持'int const',但不會因爲使用const int而被批評,或者值得改變現有代碼中的做法。 –
爲什麼這個標籤Ç –
因爲我想知道這兩種觀點 –
如果問題只是第一部分,那麼投票可能是正確的,但如果你按照問題的第二部分回答,那麼*這個投票是錯誤的* –