2

What is the difference between returning a this pointer and return by value.

I will try to explain my case with a example..

I have the following code

#include <iostream>
using namespace std;

class Count
{
    private:

        int count;      

    public:

        //Constructor
        Count():count(0) { cout << "Constructor called" << endl; }

        Count(Count& C):count(C.count)
        {  
            cout << "Copy constructor called" << endl; 
        }

        //Destructor
        ~Count() { cout << "Destructor called" << endl; }

        Count  operator ++ () {
            count++;
            Count temp;
            temp.count = count;
            return temp;
            //return *this;

        }

        //Display the value.
         void display() { 
            cout << "The value of count is " << count << endl; 
        }

};

int main()
{
Count C;
Count D;
C.display();
D.display();
D=++C;
C.display();
D.display();
return 0;
}

I have the following function in a class

Count  operator ++ () {
        count++;
        Count temp;
        temp.count = count;
        return temp;
}

when I use the above function, I see that the normal constructor is called when returning the value.

But I change the function as below

Count  operator ++ () {
        count++;
        return *this;
}

The above function calls the copy constructor when returning this pointer.

Can someone help me understand the difference.

Sathish
  • 227
  • 3
  • 11
  • Return by value typically a bit more optimal and fast. – Adam Jun 02 '15 at 15:49
  • related: [What are copy elision and return value optimization?](http://stackoverflow.com/questions/12953127/what-are-copy-elision-and-return-value-optimization) – NathanOliver Jun 02 '15 at 15:50

2 Answers2

3

Your return *this does not "return this pointer". this is a pointer, but *this is not a pointer, it is an object of Count type. Thus return *this returns the current value of *this by value, i.e. it returns a copy of your Count object.

The return temp version does the same thing, but for some unexplainable reason it first stores the current state of *this in a local variable temp and then returns temp. I don't know what the point of doing this is.

Both variants do the same thing. Both return by value in your case. Both versions call the copy constructor (at least conceptually, the call can be optimized out).

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
1

In this case, both do the same thing -- One returns a copy of the object in question, while the other manually constructs a copy of the object in question and returns it.

I should point out that the more idiomatic way to do this is

Count& operator++() 
{
        count++;
        return *this;
}

This has the advantage of returning a reference, so you can use code like

++(++C);

And have it behave as intended. With your version, that code will increment C, but then increment a copy of C.

A list of the canonical operator overload signatures is available on this Wikipedia page.

rlbond
  • 65,341
  • 56
  • 178
  • 228