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 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

Ask A Question

Stats

  • Questions 250k
  • Answers 250k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Disable ("gray out") the controls, and display an explanation (error… May 13, 2026 at 9:19 am
  • Editorial Team
    Editorial Team added an answer Response is just the text output by the upload script… May 13, 2026 at 9:19 am
  • Editorial Team
    Editorial Team added an answer What you're doing here is telling the Convert.ToDecimal function what… May 13, 2026 at 9:19 am

Related Questions

In another question, the accepted answer shows a method for reading the contents of
I am starting, and loving, TDD, however wondering about the red green light concept.
To allow access to the Win32 API from a scripting language (written in C),
What is the commonly accepted method for converting arbitrary objects to and from their

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.