more "what's this code"
Why does this return a reference to an employee class
all the variables and funcs are defined in the employee class.
Employee & Employee::operator= (const Employee & rhs)
if (this == &rhs)
itsFirstName = rhs.GetFirstName();
itsLastName = rhs.GetLastName();
itsAddress = rhs.GetAddress();
itsSalary = rhs.GetSalary();
first of all b/c : you function header is declared to return an Employee reference
second of all.....
this ------------ is a pointer to a an object that calls the function which we're in now...
and of course you wrote:
which means that you are returning a derefrenced "this", which in turn means you are returning an object to which "this" points to" not a pointer.......
little confusing ......but don't give up........pay special attention to my explanation of the "this" pointer it comes in handing, especially in operator overloading and copy constructor definitions as it does here.......
hope i helped you a bit..
You explained it pretty clearly, since it's returning itself by returning this it's returning a reference basically. What I'm wondering is in what operator overloading situations would you use * vs & vs just a plain old int, char, whatever.
i don't fully understand your last question, vs ......in terms of what???
returning ?? or using in a function???
need more detail.??
Well if you understand pointers then there's no need to go there...any how, the main advantage to using references is that within the funcion you don't have to dereference the object with the asterisk, basically!
and a simple example of "this"ness might be:
What confuses me is whether the function is trying to copy one object to another or simply pass a pointer to the original??
Jill.itsFirstName = "Jill"; //...assuming "itsFirstName" is of class string...
//...continue to fill in rest...
Employee Jack = Jill;
cout << Jack->itsFirstName; //...prints "Jill"
Anyway, there you have it...
This is the exact point I am in in C++ Primer Plus Third Edition.
The overloaded assignment operator is used when you assign one object to another.
string motto("Home of the Griz");
ditto=motto; // uses overloaded assignment operator
When initalizing an object the copy constructor is used ie
The implict implementation of the assignment operator performs a member-to-member copy. If the member is itself an object of some class, the program uses the assignment operator defined for the class to do the copying for that paticular member. Static members are unaffected.
member-by-member copying copies the values of pointers instead of the pointed to data. When the destructor is called for ditto, it deletes the string "Home of the Griz" and when in calls the destructor for motto it attempts to delete the previously deleted string.
classes that use the new operator to allocate memory pointed to by a class member
You should define a member function overloading the assignment operator
c_name & cname::operator=(const c_name & cn)
if (this == & cn_)
return *this; // done if self assignment
c_pointer = new type_name[size];
// then copy the data pointed to by cn.c_pointer to
// location pointed to by c_pointer