可能重複:
Difference between const declarations in C++const關鍵字位置
#include <iostream>
class Bar{};
void foo(const Bar x){} //l5
void foo(Bar x){} //l6
void foo(Bar const x){} //l7
////pointer functions
void foo(const Bar* x){} //l11
void foo(Bar* x){} //l12
void foo(Bar* const x){} //l13
編譯器輸出:(長話短說l5
,l6
,l7
衝突;但只有l12
,l13
衝突)
untitled.cpp:6:6: error: redefinition of ‘void foo(Bar)’
untitled.cpp:5:6: error: ‘void foo(Bar)’ previously defined here
untitled.cpp:7:6: error: redefinition of ‘void foo(Bar)’
untitled.cpp:5:6: error: ‘void foo(Bar)’ previously defined here
untitled.cpp:13:6: error: redefinition of ‘void foo(Bar*)’
untitled.cpp:12:6: error: ‘void foo(Bar*)’ previously defined here
怎麼回事?
- 什麼是每一個聲明的含義
- 爲什麼所有3點聲明與對象功能的衝突,但只有2與指針的功能呢?
- 請詳細說明衝突是
l12
和l13
之間,即使l12
不包含const
關鍵字 - 真的對不起,如果瑣碎的問題
重寫第一句:在這個特定的上下文中,與編譯器無關。然而,一般來說,'const'會修改它之前的內容,並且在修改它之後(即'int const',而不是'const int')系統地放置'const'會使代碼更具可讀性。 –