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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:38:00+00:00 2026-05-11T22:38:00+00:00

In light of the accepted answer pointing out that returning a non-const reference to

  • 0

In light of the accepted answer pointing out that returning a non-const reference to a member from a const instance method won’t compile (without a cast or making the member variable mutable), the question has become more a general best-practices discussion of const instance methods.

For posterity, here’s the original question:

If I have an object with a getter that returns a non-const reference, for example:

SomeObject& SomeOtherObject::foo(){
   return someObjectInstance;
}

Should this be made const? Obviously the call itself doesn’t modify the object, but the caller could then modify someObjectInstance which would mutate my instance of SomeOtherObject.

I guess my question really boils down to “what exactly does const on a member method mean?” Is it A) the call itself won’t mutate the object or B) no mutation of the object can occur during the call, or as a result of the returned references/pointers (with a caveat for people who do const_casts).

As an aside, I’m currently adding this where I need const calls.

 const SomeObject& SomeOtherObject::constFoo() const{
   return someObjectInstance;
}

to be on the safe side, since I’m reluctant to do

 SomeObject& SomeOtherObject::foo() const{
   return someObjectInstance;
}

even though it would make my life easier in some places.

  • 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-11T22:38:00+00:00Added an answer on May 11, 2026 at 10:38 pm

    const (when applied to a member function) is mainly useful as a means of self documenation. It is a contract with the calling code that this function will not modify the external state (i.e. have no observable side effects).

    The compilier achieves this by making all members effectively const while inside a const member function

    It is not uncommon to see code like:

    const SomeObject& SomeOtherObject::constFoo() const;
    SomeObject& SomeOtherObject::constFoo();
    

    The following won’t compile (MSVC 9 and gcc 3.4.4)

    SomeObject& SomeOtherObject::constFoo() const{
      return someObjectInstance;
    }
    

    You could hack around the above error by casting away the const:

    SomeObject& SomeOtherObject::constFoo() const{
      return (SomeObject&)someObjectInstance;
    }
    

    but this of course breaks the contract with the users of your code that you won’t be changing the SomeOtherObject instance.

    You could also make someObjectInstance mutable to be able to avoid the cast, but in the end it really isn’t any better.

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

Sidebar

Related Questions

So I have a light box that's dynamically loaded from a common.js file. (Legacy
What is the commonly accepted method for converting arbitrary objects to and from their
In another question, the accepted answer shows a method for reading the contents of
Given the following: #light //any function returning bool * 'a let foo = let
I'm using HTTP::Server::Simple::CGI for a light-weight HTTP server. That gives me a CGI object
Can someone put more light on the functions: sqlite3_reset(); sqlite3_clear_bindings() I understand that I
While I understand that in order for an iPhone application to be accepted on
In light of the recent Gawker Media password leak , I've realized that many
Please shed some light on me. How real-time system defines that events happened simultaneously?
Anyone shed any light on why the following: <form id= method=post action= /> <input

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.