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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:50:37+00:00 2026-05-22T11:50:37+00:00

A method; Sterling operator+(const Sterling& o) const { return Sterling(*this) += o; } Does

  • 0

A method;

        Sterling operator+(const Sterling& o) const {
           return Sterling(*this) += o;
         }

Does this line “Sterling(*this) += o” create a new Object in stack memory? If true, how can it return an object in the stack to outside the method?

Can I do like this:

         Sterling operator+(const Sterling& o) const {
           return *this += o;
         }

because I think *this is an object so we don’t need to create a new Object?

  • 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-05-22T11:50:38+00:00Added an answer on May 22, 2026 at 11:50 am
     Sterling operator+(const Sterling& o) const {
         return Sterling(*this) += o;
     }
    

    Creates object on the stack, but you don’t actually return this object, you return a copy of it. This function does:

    • create a temp object
    • call operator+= of the temp object with o
    • return copy of the result – note the Sterling operator+(const Sterling& o) const – if it was Sterling& operator+(const Sterling& o) const ( *note the & * ), then this would be a problem )

    Anyway, your compiler could optimize this and avoid copying of the local object, by using RVO


    And the second question:

    Sterling operator+(const Sterling& o) const {
       return *this += o;
    }
    

    This is different from the first one – the first case creates temp object and changes it, then returns it. If you do the second, this will change this and then return copy of it. But note, this object is changed!


    So, the summary – both return the same result, but the second changes this. (his would be useful, if you want to overload operator+=, not operator+ )

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

Sidebar

Related Questions

Method chaining is the practice of object methods returning the object itself in order
The method below works, but it unfortunately, this method involves creating a bitmap the
The method is public static void method(Object[] params) , how should I call it
the method is: def printa(x): return x the response is: self.response.out.write(template.render(path, {'printa':printa})) the html
This method that uses method-level generics, that parses the values from a custom POJO,
This method is published as offical example ->where(price < $minimumPrice OR price > $maximumPrice)
my method's like this:- protected string myMethod() { if (something) { string img =
Method '+systemFontSize:' not found (return type defaults to 'id') 'UIFont' may not respond to
This method takes search a search keyword and parsed mysql query, and rewrites the
which method c do this? thanks for example, a string [] = abc; 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.