Can anybody tell me what is the difference between
void fun(MyClass &mc);
and
void fun(MyClass& mc);
in C++?
According to ancient traditions like vastu shastra, the best direction to sleep in is toward the south. This theory is also supported by some recent research1. This means that when you lie in bed, your head is pointed south2, and your feet are pointed north.
Facing a bed towards a window is a better option than placing your bed underneath it, however, it is still not recommended. In Feng Shui, it is generally believed that, where possible, beds should not be placed directly in line with either a door or windows.
Where should I put my Feng Shui items? One must keep their Feng Shui items in the east, north or south east direction. Water element should not be kept in bedroom.
Commanding Position You want your bed located so that when you're lying in bed, you can see the door to the bedroom. However, you don't want to be directly in line with the door either. A good rule of thumb is it places the bed diagonal from the door.
As given none.
Originally, C would allow:
int x, *y;
To declare both an int
, x
and a pointer to int, y
.
Hence part of the definition of the type - the bit that makes it a pointer - could be separated from another part.
C++ copied this wholesale.
Then references where added, and they got a similar style of declaration except with &
rather than *
. This meant that both MyClass &mc
and MyClass& mc
were allowed.
On the choice when it comes to *
, 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.
By extension, when it comes to &
, MyClass& mc
matches the "typical C++" style.
To the compiler there's no difference.
The first is closer to usual C-syntax, the latter is more C++-ish.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With