2012-09-05 17 views
5

Alguien puede decirme cuál es la diferencia entre¿Dónde colocar el '&' en un parámetro para una función?

void fun(MyClass &mc); 

y

void fun(MyClass& mc); 

en C++?

+1

Estoy seguro de que esto se explica en la mayoría del material introductorio. –

+1

Tuve que leer esta pregunta 3 veces para darme cuenta de que esta es una pregunta de sintaxis. –

+0

Posible duplicado de [sintaxis de referencia de C++] (http://stackoverflow.com/questions/4515306/c-reference-syntax) –

Respuesta

8

Como ninguna.

Originalmente, C permitiría:

int x, *y; 

Declarar tanto un int, x y un puntero a int, y.

Por lo tanto, parte de la definición del tipo - el bit que lo convierte en un puntero - podría separarse de otra parte.

C++ copió esto al por mayor.

Entonces referencias fueron añadidos, y que tiene un estilo similar de declaración, salvo con & en lugar de *. Esto significaba que se permitían tanto MyClass &mc como MyClass& mc.

En la elección cuando se trata de *, 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.

Por extensión, cuando se trata de &, MyClass& mc coincide con el "típico C++" estilo.

5

Para el compilador no hay diferencia.

El primero está más cerca de la sintaxis C habitual, este último es más C++ - ish.

Cuestiones relacionadas