more "what's this code"

This is a discussion on more "what's this code" within the C++ Programming forums, part of the General Programming Boards category; Why does this return a reference to an employee class Code: Employee & Employee::operator= (const Employee & rhs) { if ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    9

    more "what's this code"

    Why does this return a reference to an employee class

    Code:
    Employee & Employee::operator= (const Employee & rhs)
    {
           if (this == &rhs)
           return *this;
    
           itsFirstName = rhs.GetFirstName();
           itsLastName = rhs.GetLastName();
           itsAddress = rhs.GetAddress();
           itsSalary = rhs.GetSalary();
    
           return *this;
    }
    all the variables and funcs are defined in the employee class.

  2. #2
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    first of all b/c : you function header is declared to return an Employee reference

    Employe& ......

    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:


    return *this;

    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..

    Regards,
    matheo917

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    9
    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.

  4. #4
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    i don't fully understand your last question, vs ......in terms of what???

    returning ?? or using in a function???

    need more detail.??

  5. #5
    Unregistered
    Guest
    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!


    Got it?

    and a simple example of "this"ness might be:

    Code:
    
    int main()
    {
    
    ///....some code
    
    Employee Jill;
    Jill.itsFirstName = "Jill"; //...assuming "itsFirstName" is of class string...
    
    //...continue to fill in rest...
    
    Employee Jack = Jill;
    
    cout << Jack->itsFirstName; //...prints "Jill"
    
    return 0;
    }
    What confuses me is whether the function is trying to copy one object to another or simply pass a pointer to the original??

    Strange.

    Anyway, there you have it...

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    95
    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.
    ie
    string motto("Home of the Griz");
    string ditto;
    ditto=motto; // uses overloaded assignment operator

    When initalizing an object the copy constructor is used ie
    string motto=ditto;

    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
    Code:
    c_name & cname::operator=(const c_name & cn)
    {
          if (this == & cn_)
                 return *this;  // done if self assignment
          delete c_pointer;
          c_pointer = new type_name[size];
          // then copy the data pointed to by cn.c_pointer to
          // location pointed to by c_pointer
          .....
         return *this;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Implement "Whats This" using Win32
    By hemanth.balaji in forum Windows Programming
    Replies: 1
    Last Post: 05-29-2005, 07:03 AM
  2. Lets play "Whats Wrong With That DLL?"
    By jinx in forum Windows Programming
    Replies: 9
    Last Post: 10-24-2001, 11:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21