0

The following is an excerpt from my C++ text, illustrating the syntax for declaring a class with a copy constructor.

class Student {
     int no;
     char* grade;
 public:
     Student();
     Student(int, const char*);
     Student(const Student&);
     ~Student();
     void display() const; 
 };

The copy constructor, as shown here:

Student(const Student&);

Has an ampersand after the parameter Student.

In C, and C++ as-well I believe, the ampersand character is used as a 'address of' operator for pointers. Of course, it is standard to use the & character before the pointer name, and the copy constructor uses it after, so I assume this is not the same operator.

Another use of the ampersand character I found, relates to Rvalues and Lvalues as seen here: http://www.cprogramming.com/c++11/rvalue-references-and-move-semantics-in-c++11.html

My question is not about Rvalues and Lvalues, I just want to know why the & character is placed after parameter, and what this is called and if/why it is necessary.

bigcodeszzer
  • 916
  • 1
  • 8
  • 27
  • The `&` denotes that this is a *reference* to a `const Student`, not a copy. https://en.wikipedia.org/wiki/Reference_%28C%2B%2B%29 – rlbond Oct 22 '15 at 21:11
  • Check this http://stackoverflow.com/questions/1943276/what-does-do-in-a-c-declaration you can check this link too http://www.parashift.com/c++-faq-lite/references.html – Deepanshu Oct 22 '15 at 21:14
  • Some maniacs would write this as Student(Student const&) – James Oct 22 '15 at 21:16
  • @geeksoul - Your second link didn't work. – bigcodeszzer Oct 22 '15 at 21:19
  • @bigcodeszzer, it has moved to https://isocpp.org/wiki/faq/references – Jonathan Wakely Oct 22 '15 at 21:21
  • If you have a C++ text then you should read it. – Jonathan Wakely Oct 22 '15 at 21:21
  • @JonathanWakely - See the comments under the accepted answer. – bigcodeszzer Oct 22 '15 at 21:25
  • What the textbook /should/ have said was: Be aware, C++ added another unnecessary feature to over complicate things. They are called 'references' and are pointers that you can't change. To further muddy the already majorly misunderstood concept of pointers, they even used the & operator, (and gotcha! - it has nothing to do with 'address of') – bigcodeszzer Oct 22 '15 at 21:28

1 Answers1

1

C++ has reference type that does not exist in C. & is used to define such a type.

int i = 10;
int& iref = i;

Here iref is a reference to i.

Any changes made to i is visible through iref and any changes made to iref is visible through i.

iref = 10; // Same as i = 10;
i = 20;    // Same as iref = 20;

The reference can be an lvalue reference or an rvalue reference. In the above example, iref is an lvalue reference.

int&& rref = 10;

Here rref is an rvalue reference.

You can read more about rvalue references at http://en.cppreference.com/w/cpp/language/reference.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • Is it like a processor command or does it actually take memory? What is the difference between the int& type and a pointer? – bigcodeszzer Oct 22 '15 at 21:16
  • @bigcodeszzer `int&` is implemented using `int*`, it's masked pointer, you can not change the address of. – Zereges Oct 22 '15 at 21:18
  • It is not a preprocessor operation. The difference between a pointer and reference is that a pointer can be changed to point to a different object than what it was initialized with. A reference, on the other hand, must be initialized and what it references cannot be changed after that. It's like a pointer that cannot be changed to point to anything else after it's initialized. – R Sahu Oct 22 '15 at 21:18
  • @bigcodeszzer References are mostly used for function arguments, so you can assign to the caller's variable without having to write `&var` when calling and `*ptr` when assigning. It's basically just a convenience feature. – Barmar Oct 22 '15 at 21:20
  • 1
    @bigcodeszzer Isn't all this explained in your textbook? I can't imagine it uses examples without explaining the basics behind them. – Barmar Oct 22 '15 at 21:21
  • @Barmar It did actually, I must have missed it. But still, it doesn't state it as a data type, it just lumped it under 'pass by reference' which meant something totally different in C. – bigcodeszzer Oct 22 '15 at 21:23