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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:31:53+00:00 2026-05-22T15:31:53+00:00

First, why return const? say I have friend const MyVec operator-(const MyVec& left, const

  • 0

First, why return const?
say I have

friend const MyVec operator-(const MyVec& left, const MyVec& right)

so is returning const makes me cannot do:

mva - mvb = mvc;

Second, why return const reference?
if there is:

friend const MyVec& operator++(MyVec& v)

with const I cannot: (++mva) = mvc

if it is

MyVec& operator++(MyVec& v)

I can:++(++mva) // with increment twice.

am I understanding right?

  • 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-22T15:31:54+00:00Added an answer on May 22, 2026 at 3:31 pm

    There is not any good reason to return a const object. However, there are many good reasons to return pointers or references to const objects.

    The program might have an object that is very expensive to copy, so it returns a reference. However, the object should not be changed through that reference. For example, it might be part of a sorted data structure and if its values were modified it would no longer be sorted correctly. So the const keeps it from being modified accidentally.

    Arithmetic operator functions should not return const objects, because of exactly the problems in your question.

    Dereferencing an iterator should return a const reference. That is if it is operating on a collection of const objects or possibly on a const collection. This is why class functions sometimes have two copies of a function with the second copy using a const on the function itself, like this:

    T& operator[](size_t index);
    const T& operator[](size_t index) const;
    

    The first function would be used on non-const objects and the second function would be used for const objects.

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

Sidebar

Related Questions

I am trying to write a JavaScript function that will return its first argument(function)
I know there is a method for a Python list to return the first
How can you return a string minus the first character in a string in
dict methods dict.keys() , dict.items() and dict.values() return views instead of lists. Source First
I have a stored procedure that returns two int Values. The first can not
First off, I am using Windows XP. I have multiple hard drives and it
Is there any way that you can combine predicates? Lets say I have something
I have a FAT drive, lets say H: and a directory 'work' (full path
I have a class, say, CDownloader, that reads some XML data and provides access
I have a class with a data attribute, say of type int , which

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.