0

I am confused about these terms.

Let suppose we have a class of Student then what will be the meaning of

class Student{
   public:
     Student(const Student& a){ ... }
     Student(const Student &a){ ... }
};

I want to ask the meaning of the following terms

Student& a;
Student &a;

These are used for copy constructors. The thing I know they are mostly used for deep and shallow copy constructor.

Umar Asghar
  • 3,808
  • 1
  • 36
  • 32

2 Answers2

1
int   i   =   5;
^^^   ~       =
Type  Name  Value
(int) (i)    (5)


int&   ref   =  i;
^^^^^  ~~~      =
Type   Name   Value
(int&) (ref)   (i)


int  & ref   =  i;
^^^^^^ ~~~      =
Type   Name   Value
(int&) (ref)   (i)


int   &ref   =  i;
^^^^^^^~~~      =
 Type  Name   Value
(int&) (ref)   (i)

So basically these two:

Student& a;
Student &a;

are more or less the same thing.

As for what is the purpose of a copy constructor check out:

Community
  • 1
  • 1
Drax
  • 12,682
  • 7
  • 45
  • 85
0

When I was starting to learn C, I was confused about pointer syntax. char* c;, char * c; and char *c; seemed different to me and I was terrified with so many types in C.

But it turned out that you can put an asterisk anywhere in between the 'main' type name (e.g int, char, or even a pointer, etc) and the variable name. That means, all these declarations are the same.

This also applies to references in C++. Actually, it depends of the developer's coding style while the meaning doesn't change.

ForceBru
  • 43,482
  • 10
  • 63
  • 98