回答
因爲沒有。
本來,C將使:
int x, *y;
要聲明既是int
,x
和指針爲int,y
。
因此,類型定義的一部分 - 使它成爲指針的位 - 可以與另一部分分開。
C++複製此批發。
然後引用添加的地方,他們得到了類似的聲明風格,除了&
而不是*
。這意味着允許MyClass &mc
和MyClass& mc
。
上上之選,當談到*
,Strousup wrote:
Both are "right" in the sense that both are valid C and C++ and both have exactly the same meaning. As far as the language definitions and the compilers are concerned we could just as well say "int*p;" or "int * p;"
The choice between "int* p;" and "int *p;" is not about right and wrong, but about style and emphasis. C emphasized expressions; declarations were often considered little more than a necessary evil. C++, on the other hand, has a heavy emphasis on types.
A "typical C programmer" writes "int *p;" and explains it "*p is what is the int" emphasizing syntax, and may point to the C (and C++) declaration grammar to argue for the correctness of the style. Indeed, the * binds to the name p in the grammar.
A "typical C++ programmer" writes "int* p;" and explains it "p is a pointer to an int" emphasizing type. Indeed the type of p is int*. I clearly prefer that emphasis and see it as important for using the more advanced parts of C++ well.
The critical confusion comes (only) when people try to declare several pointers with a single declaration:
int* p, p1; // probable error: p1 is not an int*
Placing the * closer to the name does not make this kind of error significantly less likely.
int *p, p1; // probable error?
Declaring one name per declaration minimizes the problem - in particular when we initialize the variables. People are far less likely to write:
int* p = &i; int p1 = p; // error: int initialized by int*
And if they do, the compiler will complain.
Whenever something can be done in two ways, someone will be confused. Whenever something is a matter of taste, discussions can drag on forever. Stick to one pointer per declaration and always initialize variables and the source of confusion disappears. See The Design and Evolution of C++ for a longer discussion of the C declaration syntax.
推而廣之,當它涉及到&
,MyClass& mc
「典型C++」 的風格相匹配。
編譯器沒有區別。
第一個更接近通常的C語法,後者更多的是C++ - ish。
- 1. 在C++中將默認值參數放置在變長函數中的位置?
- 2. 將封裝函數放置在其他腳本中的位置?
- 3. 將for_each調用的函數放在類中的位置?
- 4. 在bash中的一個函數中設置位置參數
- 5. xargs - 將參數放在命令中的不同位置
- 6. 將定製函數放在Zend Framework中的位置1.10
- 7. C++函數默認參數的位置
- 8. 按位置引用函數的參數?
- 9. 函數參數的內存位置
- 10. 在CakePHP中放置自定義函數的位置
- 11. 在express.js中放置常用函數的位置?
- 12. 在文檔中放置javascript函數定義的位置?
- 13. 在Code Igniter中放置通用函數的位置?
- 14. 在角度指令中放置輔助函數的位置?
- 15. 如何在Swift 3中將函數設置爲函數參數
- 16. 的位置參數
- 17. 函數的放置
- 18. LISP:在函子位置參數非法
- 19. LLVM:獲取函數參數位置(ABI)
- 20. 與dlsym導入的函數中的參數錯誤位置
- 21. 將函數放在函數中javascript
- 22. 將數據集文件放置在gnuplot中的位置
- 23. 將數據庫文件放置在Visual Studio中的位置
- 24. 如何在循環中放置函數參數
- 25. 如何將數組的總元素作爲函數的位置參數傳遞?
- 26. C/C++:函數參數列表中的`const`位置
- 27. 在C++中將函數參數作爲函數參數傳遞
- 28. 確定參數在drools中的位置
- 29. 將文件放在Visual Studio中作爲命令行參數的位置
- 30. 將參數從JavaScript放入JSF2的EL函數調用中
我相信這在大多數介紹性材料中都有解釋。 –
爲了認識到這是一個語法問題,我不得不閱讀這個問題3次。 – 2012-09-05 11:59:28
可能的[C++參考語法]重複(http://stackoverflow.com/questions/4515306/c-reference-syntax) –