[英]Why is it not allowed to assign const char * to const variable?
[英]Assigning to a char * const * variable from a char * * variable - why is it allowed?
我對C ++(Visual Studio 2012編譯器)的以下行為感到驚訝。
char * * PointerToPointerToChar = NULL;
char * const * PointerToConstPointerToChar = NULL;
char const * * PointerToPointerToConstChar = NULL;
PointerToPointerToConstChar = PointerToPointerToChar; // Assignment 1: Gives compiler error as I would expect
PointerToConstPointerToChar = PointerToPointerToChar; // Assignment 2: NO COMPILER ERROR ???
PointerToPointerToChar = PointerToPointerToConstChar; // Assignment 3: Gives compiler error as I would expect
PointerToPointerToChar = PointerToConstPointerToChar; // Assignment 4: Gives compiler error as I would expect
我了解C ++中的const關鍵字,尤其是它的位置如何影響const(左側的實體)。
似乎編譯器試圖在任一間接級別 (分配3和4)保護RHS變量的用戶免受const剝離別名的影響。 但是LHS僅在一個間接級別(分配1)而不在另一個間接級別(分配2)受到保護,免受const剝離別名的侵害。 換句話說,假設編譯器阻止了以下操作
PointerToPointerToConstChar = PointerToPointerToChar; // Assignment 1: Gives compiler error as I would expect
PointerToPointerToChar[0][0] = 'A'; // The user of the LHS variable was effectively lied to about the constness of the *characters* - second level of indirection
那么為什么編譯器也無法阻止以下操作
PointerToConstPointerToChar = PointerToPointerToChar; // Assignment 2: NO COMPILER ERROR ???
PointerToPointerToChar[0] = NULL; // The user of the LHS variable was effectively lied to about the constness of the *pointers* - first level of indirection
這是正確的C ++嗎?如果是,則依據是什么? 似乎不一致。 謝謝!
type const*
並不意味着該值不會改變,這意味着您無法使用該指針進行更改。
因為如果可以這樣做,則可以將任何const char*
分配給*PointerToPointerToConstChar
而PointerToPointerToChar
依靠它為普通char*
。 如果允許的話:
PointerToPointerToConstChar = PointerToPointerToChar; // assume PointerToPointerToChar is pointing to a valid memory block
const char str[] = "Hello world!";
*PointerToPointerToConstChar = str;
(*PointerToPointerToChar)[0] = 'X'; // Oops, we just modified a const array
將char**
分配給char* const*
類似於將char*
分配給const char*
。 這是很合理的。
char array[] = "foo";
char* ptr1 = array; // Can change array through ptr1
char const* ptr2 = ptr1; // Can't change array through ptr2
使用char**
和char* const*
使可以更改和不能更改的內容更加復雜。
char** ptrptr1 = &ptr1;
// Can change where ptr1 points to through ptrptr1
*ptrptr1 = <some other char*>; // OK
// Can change the value of what ptr1 points to through ptrptr1.
(*ptrptr1)[0] = 'x'; // OK
char* const* ptrptr2 = ptr1;
// Can't change where ptr1 points to through ptrptr2
*ptrptr2 = <some other char*>; // Not OK
// Can change the value of what ptr1 points to through ptrptr2.
// (*ptrptr2) is still of type 'char*'.
(*ptrptr2)[0] = 'x'; // OK
您有一個指向const
的指針。 指針不是const
,而是它指向的內容。 您可以為該指針分配一個指向非const
的指針。 那也行。 指針不是const,在這種情況下const
只是告訴我們,指向的對象不會通過此指針更改。 它不必在其他任何地方都是const。
char* c = someCharPointerSomewhere;
PointerToConstPointerToChar = PointerToPointerToChar;
*PointerToConstPointerToChar = c // const violation
*PointerToPointerToChar = c // this is fine
// now *PointerToConstPointerToChar will be c as well.
那為什么不能分配一個指向const
的指針呢? 好吧,指向const
的指針與指向non const
的指針是不同的類型。 我們上面的隱式轉換不適用於這種情況。 您可以使用此指針,使其指向的對象實際上是const
。 然后,原始指針將允許間接修改const值。
const char c = 'c';
PointerToPointerToConstChar = PointerToPointerToChar;
*PointerToPointerToConstChar = c;
**PointerToPointerToChar = 'a'; // const violation!
當您僅閱讀說明但花些時間仔細考慮時,這會有些混亂。 這樣在邏輯上確實是一致的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.