Thread: Question about operator overloading

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    228

    Question about operator overloading

    Hi,
    I wrote the this following simple, straightforward code (for learning purpuses only), and played with it a little bit.
    there's something I wanted to ask though.
    first here's the code:

    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    
    //class Point
    class Point
    {
    private:
        int X;
        int Y;
    public:
        //constructors
        Point(int x=0, int y=0):X(x), Y(y){cout<<"Ctor called to construct "<<*this<<endl;}
        Point(const Point& point);
        //destructor
        ~Point(){cout<<"Destructor called to destruct "<<*this<<endl;}
        //operators overloading
        const Point& operator+(const Point& p)const;
        Point& operator=(const Point& p);
        friend ostream& operator<<(ostream& os, const Point& p);
    };
    
    //overloading << operator
    ostream& operator<<(ostream& os, const Point& p)
    {
        return os<<"("<<p.X<<","<<p.Y<<")";
    }
    
    //overloading + operator
    const Point& Point::operator+(const Point& p)const
    {
        cout<<"Operator + called, ";
        cout<<"Caller is: "<<*this<<endl;
        cout<<"Adding "<<p<<" to "<<*this<<endl;
        return Point(X+p.X, Y+p.Y);
    }
    
    //overloading = operator
    Point& Point::operator=(const Point& p)
    {
        cout<<"Operator = called, ";
        cout<<"Assigning "<<p<<" to "<<*this<<endl;
    
        X=p.X;
        Y=p.Y;
        return *this;
    }
    
    //the copy constructor
    Point::Point(const Point& point)
    {
        cout<<"CCtor called: ";
        cout<<"Copying the point "<<point<<endl;
        X=point.X;
        Y=point.Y;
    }
    
    
    //main
    int main()
    {
        Point p0, p1(1,1), p2(2,2);
        p0=p1+p2;
    }
    Now, I know, that the + operator should return a Point object by value, meaning its prototype should be:
    Code:
    Point Point::operator+(const Point& p)const
    but I wanted to see what output I'll get if I change it.

    so this is the output I get for the above main() function, and the question is in it.

    Code:
    Ctor called to construct (0,0)
    Ctor called to construct (1,1)
    Ctor called to construct (2,2)
    Operator + called, Caller is: (1,1)
    Adding (2,2) to (1,1)
    Ctor called to construct (3,3)
    Destructor called to destruct (3,3) //temp object was destroyed here
    Operator = called, Assigning (3,3) to (0,0) //so how can it be assigned to (0,0) here?
    Destructor called to destruct (2,2)
    Destructor called to destruct (1,1)
    Destructor called to destruct (3,3)
    thanks in advanced!

    P.S, any additional useful tips are very welcome.
    like, for example, when is it a good idea to return by value and when by reference?
    and what about concatenation? where does it need special consideration?
    Last edited by Absurd; 06-22-2013 at 07:18 AM.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    You're returning a const reference to a temporary. That's not allowed, because as you can see, the temporary is destroyed before the function returns. However, in this small program, nothing bad happens, because even though the temporary variable is destroyed and is no longer tracked, nothing has a chance to overwrite it, so the data is still there.

    So what you have is undefined behavior. It might work on your compiler, maybe even most compilers, but it is not legal C++.



    In general, for return values, it's fast and convenient to return by value, unless you're just returning a reference that's a function argument or to a global variable.
    Last edited by King Mir; 06-22-2013 at 08:09 AM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Operator overloading question
    By ranish in forum C++ Programming
    Replies: 2
    Last Post: 04-27-2010, 01:20 PM
  2. question about operator overloading
    By dakotabk in forum C++ Programming
    Replies: 1
    Last Post: 05-13-2009, 11:02 AM
  3. Question about operator overloading
    By h3ro in forum C++ Programming
    Replies: 3
    Last Post: 04-15-2008, 01:02 PM
  4. Operator* Overloading question?
    By pc_dude23 in forum C++ Programming
    Replies: 4
    Last Post: 10-06-2006, 12:17 PM
  5. Operator overloading question
    By Great Satchmo in forum C++ Programming
    Replies: 1
    Last Post: 09-18-2004, 07:41 PM