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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:12:53+00:00 2026-06-06T07:12:53+00:00

I have overloaded the + operator like this class sample { private : int

  • 0

I have overloaded the + operator like this

class sample
{
private : 
  int x;
public :
  sample(int x1 =0)
  {
    x = x1;
  }

  sample operator+(sample s);
};

sample sample::operator+(sample s)
{
  x = x + s.x;
  return *this;
}

int  main()
{
  sample s1(10);
  sample s2;
  s2 = s2 + s1;
  return 0;    
}

Is this correct?
My question is If I want to add two different sample objects how will I overloaded the opeartor; e.g for s = s1 + s2;

I feel like doing s = s + s1 + s2 with the existing implementation.

  • 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-06T07:12:55+00:00Added an answer on June 6, 2026 at 7:12 am

    Using friend operator overload should do the trick for you and is a common way to define binary operators, just add:

    friend sample operator+(const sample& a, const sample& b); //in class
    
    sample operator+(const sample& a, const sample& b) { //outside the class
        return sample(a.x + b.x);
    }
    

    If you want it to remain a member, (which has downsides in some rare scenarios, and no upsides), you have to make the operator a const function:

    sample operator+(sample s) const; //in class
    
    sample sample::operator+(const sample& b) const { //outside the class
        return sample(this->x + b.x);
    }
    

    Either of these will allow operator chaining. The reason your previous s = s + s1 + s2 was failing, is that the s + s1 would execute and return a temporary sample object. Then, it would attempt to add s2 to that sample. However, temporaries can only be const references[1], and as such, can only use const member functions. Since your operator+ member function is not a const function, you cannot use that function on the const temporary. Note that to make it const, I had to rewrite it, since your version modifies the object on the left side of the +.

    [1]with exceptions aren’t particularly relevant here, namely rvalues

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

Sidebar

Related Questions

I'm having an issue. I have a class with an overloaded operator like this..
Hi I have a code like this, I think both the friend overloaded operator
I have my class, where I overloaded the ! operator: class obj { public:
Say I have a class like so: class Ingredient { public: friend istream& operator>>(istream&
Let's say we have this class A: class A { public: int a; A(int
I have a class which does not have copy constructor or operator= overloaded. The
I have an overloaded << operator from my reckful class implemented as follows: ostream&
I want to write a file_handle class like this #include <stdio.h> #include <iostream> #include
I have a class that pretends to be an int, so it has overloaded
I have a logging class which has operator<< overloaded. So I can do things

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.