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 8854813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:58:14+00:00 2026-06-14T13:58:14+00:00

When a class overloads operator+ , should it be declared const since it does

  • 0

When a class overloads operator+, should it be declared const since it does not do any assignment on the object?
Also, I know that operator= and operator+= return a reference because an assignment is made. But, what about operator+? When I implement it should I make a copy of the current object, add the given object to that, and return that value?

Here is what I have:

class Point
{
public:
    int x, int y;

    Point& operator += (const Point& other) {
        X += other.x;
        Y += other.y;
        return *this;
    }

    // The above seems pretty straightforward to me, but what about this?:
    Point operator + (const Point& other) const { // Should this be const?
        Point copy;
        copy.x = x + other.x;
        copy.y = y + other.y;
        return copy;
    }
};

Is this a correct implementation of the operator+? Or is there something I am overlooking that could cause trouble or unwanted/undefined behavior?

  • 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-14T13:58:15+00:00Added an answer on June 14, 2026 at 1:58 pm

    Better than that, you should make it a free function:

    Point operator+( Point lhs, const Point& rhs ) { // lhs is a copy
        lhs += rhs;
        return lhs;
    }
    

    But yes, if you leave it as a member function it should be const as it does not modify the left hand side object.

    Regarding whether to return a reference or a copy, the advice for operator overloading is do as fundamental types do (i.e. do as ints do). In this case, addition for two integers returns a separate integer that is not a reference to neither one of the inputs.

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

Sidebar

Related Questions

Possible Duplicate: Overload ++ operator Say i have a class in which i overloads
I've got a simple class Currency with overloaded operator<<. I don't know how can
I have always been taught that non-primitive types should be passed by const reference
From what I remember from my C++ class, the professor said that operator overloading
Possible Duplicate: Where should non-member operator overloads be placed? While browsing on SO, I
I want to overload operator<< for my class. Should I add this overloaded definition
I am trying to overload the subscript operator ([]) on an abstract class, the
I'm trying to overload the '--' postfix operator. I have this code: class Counter
Possible Duplicate: How do I overload the square-bracket operator in C#? For a class
I have a Card class and I want to overload the > operator to

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.