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

  • Home
  • SEARCH
  • 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 8452781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:35:26+00:00 2026-06-10T11:35:26+00:00

Given the example code: class Integer { int i_; public: Integer(int i) : i_(i)

  • 0

Given the example code:

class Integer
{
    int i_;
public:
    Integer(int i) : i_(i) {}
    const Integer operator+(const Integer &arg) const { return Integer(i_ + arg.i_); }
};

I started wondering whether operator+() should actually return a const Integer or not. Bruce Eckel in “Thinking in C++” provides examples for operator overloading for a similar case and seems to favor the const modifier on the return type. Should it be used and why?

On the other hand, let’s try to use this class:

int main()
{
    Integer a(1), b(2);
    Integer c = a + b;
}

When creating c as a sum of a and b the compiler will most likely perform copy elision and Integer::operator+() will create its result in the spot occupied by c (see “Want speed? Pass by value“) directly. But when the return type of Integer::operator+() is declared const, won’t that force the compiler to perform copying since the target is non-const?

  • 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-10T11:35:28+00:00Added an answer on June 10, 2026 at 11:35 am

    Returning an Integer const protects against things like:

    (a + b) = c;
    

    I’ve never found this to be too much of a problem, but forbidding it
    does make the type behave more like the built-in types. It shouldn’t
    have any impact with regards to return value optimization.

    If the class is extremely expensive to copy, and can be made to support
    rvalue references effectively, you might want to avoid the const
    because of these, but in practice, I suspect that such classes are rare
    enough that you can afford to neglect them.

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

Sidebar

Related Questions

Given the next code example, I'm unable to free the parameter const char* expression
Given the example source code below, is it possible for someone to see the
For example, my goal is to test the code given here: PHP script that
When I try to compile the example code given below, I get the following
Please pardon me for a n00bish question. Please consider the following code: public class
Given: a class with properties like SomeNumber that, internally can be null (i.e. int?
Consider the following Java code: public class Test { private Foo< String, String >
I know how to do this... I'll give example code below. But I can't
Could anyone give me example code to access the command line through VBScript? I
Can anybody give an example of c++ code that can easily convert a decimal

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.