3

if I wanted to return "this" from a class member function as reference would this piece of code be correct ?

Record& operator=(const Record& that) {
    m_name = that.m_name;
    return *this;
}

Shouldn't i just use "return this" ?

Thanks for help :)

Sebastian Dusza
  • 2,470
  • 2
  • 30
  • 54

3 Answers3

8

Yup, that's correct.

Return this wouldn't work since this is a pointer. (The reason it's a pointer and not a reference is because references weren't introduced into the language until after classes.)

In this specific case, if you're just going to assign the members anyway you shouldn't write a copy assignment operator; the default will do the same. When you manage some resource (and invoke the Rule of Three), you'd want to use the copy-and-swap idiom.

GManNickG
  • 494,350
  • 52
  • 494
  • 543
  • Perhaps a link would help http://www.java2s.com/Tutorial/Cpp/0180__Class/Returningthedereferencedthispointer.htm – anijhaw Sep 20 '10 at 18:45
  • Hence 'return this' would require 'Record*' as the return type in the declaration of the operator – Steve Townsend Sep 20 '10 at 18:46
  • 1
    @anijhaw: I don't see what for. :) And even if, I never particularity enjoyed that site. As far as I can tell, it's just code snippets. (And if I recall, some bad ones too.) – GManNickG Sep 20 '10 at 18:47
  • 1
    This is in the C++ FAQ: http://www.parashift.com/c++-faq-lite/references.html#faq-8.4 – Matt K Sep 20 '10 at 18:48
4

Your code is correct, but your comment shouldn't I just use "return this" is wrong (or, more accurately, the answer is "no, you shouldn't, and if you try, any compiler that works even halfway correctly compiler will stop you and give an error message.")

Given a class like:

class T { 
    void X() {}
    void Y() const {}
};

In T::X, this will have the type T *const, and in T::Y, this will have the type T const *const. Either way, it's a pointer to T, not a reference to T. To get a reference to T (for returning or any other purpose) you need to use *this.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • 1
    (`this` not const-qualified, by the way. It is an rvalue, though.) – GManNickG Sep 20 '10 at 19:01
  • 1
    @GMan: right -- yet another oddity due to C++'s long and varied heritage. Back before you could overload `operator new` and `operator delete`, you got (kind of) the same effect by allocating a block of memory and assigning its value to `this` in the ctor. That's no longer allowed, but qualification to make that explicit has never been added... – Jerry Coffin Sep 20 '10 at 19:13
1

It may not matter for this specific case, but it is a good practice to have self assignment guard in the copy-assignment operator.

Record& operator=(const Record& that) {
    if( &that != this ) {
        m_name = that.m_name;
    }
    return *this;
}

This guards against statements like

Record r;
// do something with r
r = r;     // this is protected

It would be important when the class is doing some resource (e.g. dynamically allocated memory) management.

Arun
  • 19,750
  • 10
  • 51
  • 60
  • If the class were managing a resource, it should use the [copy-and-swap idiom](http://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom). – GManNickG Sep 21 '10 at 00:15