0

i have a complex function definition written in c++. It is the first time i have come across such a complex function definition and i am having trouble understanding the meaning of it.

Here it is

t_group& t_group::operator=(const t_group &a)
{

}

specifically i need to know what

operator=(const t_group &a)

mean ?

crowso
  • 2,049
  • 9
  • 33
  • 38
  • 3
    Why down-voted? The question is properly formulated. May be too easy for some arrogant expert ... but remember all of you where "child" somehow or sometime. – Emilio Garavaglia Jun 03 '12 at 15:30
  • +1 It's not like operator overloading is a standard, or even necessary, feature of all computer languages. Also, operator overloading has a lot of semantics you have to learn sometime; it's not like functions where the parameters are pretty much arbitrary. – Mike DeSimone Jun 03 '12 at 22:42
  • http://en.wikipedia.org/wiki/Assignment_operator_%28C%2B%2B%29 – wroniasty Jun 03 '12 at 15:17

1 Answers1

5

Here's the breakdown:

t_group&

The function returns a reference to a t_group.

t_group::

The function is in the t_group namespace. Since t_group is the name of a struct, union, or class, it is a member of t_group.

operator=

The function is an overload of the = operator. Since it is a method, the object is the left-hand-side of the = operator.

(const t_group &a)

This is the parameter to the function: it's the right-hand-side of the = operator. This says the right-hand-side is a const reference to a t_group, which means the function will not alter the t_group.

Taken together, this is the copy assignment operation for the t_group class. It is invoked by code like:

t_group a, b;
b = a;

The latter line is equivalent to b.operator=(a);.

P.S. assignment operator functions typically end with return *this;. This is so you can chain the assignments (e.g. a = b = c) just like the regular = operator.

Mike DeSimone
  • 41,631
  • 10
  • 72
  • 96