4

Here I am taking an example of overloading the increment operator:

 class Digit
 {
     int m_digit;

  public:
      Digit (int value)   //constructor
          {
            m_digit = value;
          }

      Digit& operator++();

      int ret_dig (){return m_digit;}
};

Digit& Digit::operator++()
{
   if (m_digit == 9)
        m_digit = 0;

   else  ++m_digit;
    return *this;
 }

int main ()
{
   Digit my_dig (5);
   ++my_dig;
 return 0;
 }

I have been told that local variables can't be returned. Isn't "this" a local variable? Here is what I think:

A pointer of type Digit is attached to the member function (overloaded operator function). When compiler sees the line ++my_dig, that is an instance of Digit class, it calls the member function. The address of the instance my_dig is passed as argument to the function and there is a hidden "const Digit*" named "this" to catch the argument. "this" is dereferenced(implicitly) to access m_digit, that is a member variable of class Digit. All the increment or wrapping is done inside the function and a reference to dereferenced "this" is then returned to the caller. Yes, this is the point to my confusion. If "this" is a local variable of type const Digit*, shouldn't it contain garbage when returned because "this" is going out of scope where the block ends haaa?

Mark Lakata
  • 19,989
  • 5
  • 106
  • 123
user5241471
  • 105
  • 5
  • You can return a local variable. There is very little to stop you from doing it.The end result might not be what you expected or wanted, but you can do it. – user4581301 Sep 09 '15 at 04:53
  • The scope of a pointer variable does not affect the lifetime of the object it points to. – molbdnilo Sep 09 '15 at 05:55

3 Answers3

5

this is an implicit parameter to all member functions which points to the object itself -- which has a strictly longer lifetime than the method. The parameter itself is a local variable, but the object that it points to exists outside the method.

In this case the object is created on the first line of your main function and then lives until the main method exits. Thus the object is safely alive throughout the call to operator++!

Buddy
  • 10,874
  • 5
  • 41
  • 58
  • "this" is a parameter, right? All parameters are local to the function, but you are saying "this" is not local, can you please elaborate :-) – user5241471 Sep 09 '15 at 04:46
  • Yes, `this` is an implicit parameter to all member functions. But it points to the object itself, which has a strictly longer lifetime than the function. – Buddy Sep 09 '15 at 04:58
  • And dereferencing "this" will draw back the object. Thus, reference to the object is again sent back after incrementing the copy of member variable created for my_dig (in this case), right? – user5241471 Sep 09 '15 at 05:03
  • @user5241471 an interesting test of what is `this` is seeing what happens when you try to increment `this`. You'll find the behaviour is a little bit different then a normal passed-in parameter – user4581301 Sep 09 '15 at 05:24
  • 1
    I don't think that the object has a strictly longer lifetime than the method call. `delete this`, while unusual is a valid statement. – MSalters Sep 09 '15 at 08:11
  • 1
    @MSalters: what you say is true, but I was trying not to overly complicate the answer with unusual edge cases. – Buddy Sep 09 '15 at 14:58
2

this is a local variable, but it is initialized by &my_dig when it enters the method. The value of &my_dig has the same lifetime as my_dig, which ends at the end of the main block. So while this goes out of scope, the value of this is still valid.

Mark Lakata
  • 19,989
  • 5
  • 106
  • 123
2

The *this is the object and not a local variable. Any temporary objects which the compiler deems necessary to allocate, such as those needed to complete a expression, has a lifetime to the end of the expression.

This becomes interesting for objects which has a destrctor, as the destructor run not when the individual parts of the expression is completed, but when the entire expression completes -- hence your *this variable will survive just sufficiently long to complete what is needed.

Have a look at this question for deeper discussion

Community
  • 1
  • 1
Soren
  • 14,402
  • 4
  • 41
  • 67