Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8368961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:31:18+00:00 2026-06-09T13:31:18+00:00

I was doing this as a personal exercise and wanted to make sure I

  • 0

I was doing this as a personal exercise and wanted to make sure I got this right and understand it correctly. I have a coordinate class with members row and column. I wanted to overload the + and += operator. Here is my code:

Coordinate& Coordinate :: operator+= (const Coordinate& rhs){
    this->m_Row += rhs.m_Row;
    this->m_Column += rhs.m_Column;

    return *this;
}

Coordinate& operator+ (const Coordinate& lhs, const Coordinate& rhs) {
    return Coordinate(lhs) += rhs;
}

where

friend Coordinate& operator + (const Coordinate& lhs, const Coordinate& rhs);

is a friend function defined in Coordinate class.

Are there any pitfalls to this code ?

Here is my understanding of how these work:

operator += 

Add rhs m_Row and m_Column to this members. Return a reference to the object pointed by this pointer and thereby avoid creating another object due to copy constructor.

operator +

Create a local object of lhs (since lhs is a constant and we don’t want to modify its contents) using the copy constructor (lets call it localObj). Invoke the += member operator on localObj which performs the addition. Return a reference to this localObj so that we don’t create another object due to copy constructor, otherwise.

Now, the very last statement concerns me, since I am returning a reference to a local object. As soon as the function (operator +) goes out of scope, localObj will be destroyed and returned reference will point to an object which has been destroyed. Am I correct in understanding this ?

If so, how should I fix it ??

EDIT:
After all the answers and what I learnt: here is what my Coordinate class looks like now: http://rextester.com/MJJI7394

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T13:31:19+00:00Added an answer on June 9, 2026 at 1:31 pm

    You are right to be worried, you are returning a reference to a temporary here:

    Coordinate& operator+ (const Coordinate& lhs, const Coordinate& rhs) {
        return Coordinate(lhs) += rhs;
    }
    

    You need to return a Coordinate by value, for example like this:

    Coordinate operator+ (Coordinate lhs, const Coordinate& rhs) {
        return lhs += rhs;
    }
    

    In the example above, we make a copy of the first parameter instead of taking a reference and then copying in the body of the function. Then we return the result of += on that, by value.

    With this setup, there is no need to declare operator+ as a friend.

    See this SO link for more information, and thanks to @Blastfurnace for pointing it out.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing this currently, class Page { // variable to hold DBC class
I'm doing this interface where I have a lot of buttons that are just
I hope I am doing this correctly.. Using this code I am grabbing records
I'm doing this with a very simple class, just to learn, but I'm not
I have trouble doing this... You may want to get the same result as
I have a personal caching class, which can be seen here ( based off
this has interested me purely as research and personal development. i have a namespaced
OK... This is confusing me...I have a way I can think of doing this...but
Alright, I have received some helpful information on this personal site I am working
For a personal project I'm working on right now I want to make a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.