Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ampersand (&) at the end of variable etc

I am a C++ noob and i've a problem of understanding c++ syntax in a code. Now I am quite confused.

class date { private: int day, month, year; int correct_date( void ); public: void set_date( int d, int m, int y ); void actual( void ); void print( void ); void inc( void ); friend int date_ok( const date& ); }; 

Regarding to the '&' character, I understand its general usage as a reference, address and logical operator...

for example int *Y = &X

What is the meaning of an & operator at end of parameter?

friend int date_ok( const date& ); 

Thanks

edit:

Thanks for the answers. If I have understood this correctly, the variable name was simply omitted because it is just a prototype. For the prototype I don't need the variable name, it's optional. Is that correct?

However, for the definition of the function I definitely need the variable name, right?

like image 811
snipor Avatar asked Jan 01 '14 00:01

snipor


People also ask

Why is it called the ampersand?

Etymology. The term ampersand is a corruption of and (&) per se and, which literally means "(the character) & by itself (is the word) and." The symbol & is derived from the ligature of ET or et, which is the Latin word for "and."

Is ampersand grammatically correct?

An ampersand (&) is a typographical symbol that is rarely used in formal writing. It is read aloud as the word and and is used as a substitute for that word in informal writing and in the names of products or businesses.

What is this symbol called &?

What is an &? & is called an ampersand symbol (pronounced “AM- per-sand”). Essentially, it means “and”. It is used both (a) in the body of the paper as part of a citation and (b) at the end of the paper as part of a reference.

How do you write an &?

It is actually a ligature (two characters combined) an e and t from the Latin word et, meaning “and”. With many variations, there are two main ways of writing the ampersand: the traditional version (&), and the style that looks more like an E or an et.


2 Answers

const date& being accepted by the method date_ok means that date_ok takes a reference of type const date. It works similar to pointers, except that the syntax is slightly more .. sugary

in your example, int* Y = &x makes Y a pointer of type int * and then assigns it the address of x. And when I would like to change the value of "whatever it is at the address pointed by Y" I say *Y = 200;

so,

int x = 300; int *Y = &x; *Y = 200; // now x = 200 cout << x; // prints 200 

Instead now I use a reference

int x = 300; int& Y = x; Y = 200; // now x = 200 cout << x; // prints 200 
like image 105
Aniket Inge Avatar answered Sep 28 '22 01:09

Aniket Inge


In this context, & is not an operator. It is part of the type.

For any given type T, the type T& is a "reference to T".

The symbol & in fact has three meanings in C++, and it's important to recognise those different meanings.

  • "address of" when applied to an expression
  • "reference" when part of a type
  • "bitwise AND" when applied to two numbers

Similarly, * has at least three meanings, and once you've grasped those, you'll have pointers and references down. :-)

If I have understood this correctly, the variable name simply was omitted there because it is just the prototype. And for the prototype i don't need the variable name, it's optional. Is that correct?

Yes.

However, for the definition of the function I need definitely the variable name, right?

No. Although you'll usually want it (otherwise what's the point?!) there are some circumstances in which you don't, usually when you've only introduced the parameter to engage in overload-related trickery.

But speaking purely technically you can omit the argument name from the declaration and/or the definition as you wish.

like image 25
Lightness Races in Orbit Avatar answered Sep 27 '22 23:09

Lightness Races in Orbit