Ai có thể cho tôi biết sự khác biệt giữaVị trí đặt '&' trong tham số cho hàm?
void fun(MyClass &mc);
và
void fun(MyClass& mc);
trong C++ là gì?
Ai có thể cho tôi biết sự khác biệt giữaVị trí đặt '&' trong tham số cho hàm?
void fun(MyClass &mc);
và
void fun(MyClass& mc);
trong C++ là gì?
Không được cung cấp.
Nguyên, C sẽ cho phép:
int x, *y;
Để tuyên bố cả một int
, x
và một con trỏ đến int, y
.
Do đó một phần của định nghĩa về loại - bit làm cho nó thành con trỏ - có thể được tách ra khỏi một phần khác.
C++ đã sao chép bán buôn này.
Sau đó, tài liệu tham khảo được thêm vào và chúng có kiểu khai báo tương tự ngoại trừ &
thay vì *
. Điều này có nghĩa là cho phép cả hai số MyClass &mc
và MyClass& mc
.
Trên lựa chọn khi nói đến *
, 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.
Suy rộng ra, khi nói đến &
, MyClass& mc
phù hợp với "điển hình C++" phong cách.
Trình biên dịch không có sự khác biệt.
Đầu tiên gần với cú pháp C thông thường hơn, cú pháp thứ hai là C++ - ish.
Tôi chắc chắn điều này được giải thích trong hầu hết các tài liệu giới thiệu. –
Tôi đã phải đọc câu hỏi này 3 lần để nhận ra rằng đây là một câu hỏi cú pháp. –
Có thể trùng lặp cú pháp tham chiếu [C++] (http://stackoverflow.com/questions/4515306/c-reference-syntax) –