operator overloading

Here employee_count is a class, In this context, could somebody explain the meaning of & in following statement(operator overloading)
 
 employee_count & operator=(const employee_count  & count);
It means the parameter is passed by reference and the return value is returned by reference.
http://www.cplusplus.com/doc/tutorial/functions/#reference
Last edited on
It is an assignment operator in a class, which returns a reference to itself.
Something like:

1
2
3
4
5
6
class employee_count
{
  ...

  employee_count& operator=(const employee_count &count);
};


It would get used in both these statements:

1
2
employee_count c = d;
d = c;


Typically you write it thus:
1
2
3
4
5
6
7
8
employee_count& employee_count::operator=(const employee_count& a)
{
  xyz = a.xyz; // assign every element of a to this
  abc = a.abc;
  ...

  return *this;
}


Ref:
http://en.wikipedia.org/wiki/Assignment_operator_(C%2B%2B)
http://www.cplusplus.com/articles/y8hv0pDG/
It would get used in both these statements:
employee_count c = d;
No, it would not. This is initialization, not assigment. In this case copy constructor will be called, not the assigment operator.

To add to the answer: assigment operator returning reference to the object is used to chain operators:
1
2
employee_count a, b, c;
a = b = c; //Assigns value of c to a and b 

1
2
3
4
while ( (a = get_count()) != E_COUNT_NULL)
//use a

//Reads data in a and operates on ituntill some value equal to E_COUNT_NULL is read 
Last edited on
yes,
employee_count c = d;
is equivalent to
employee_count c(d);
which can be any of:
1
2
3
employee_count::employee_count(const employee_count&);
employee_count::employee_count(employee_count&);
employee_count::employee_count(employee_count);
Topic archived. No new replies allowed.