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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:06:20+00:00 2026-06-01T07:06:20+00:00

Const functions don’t seem to care if you dereference a member-pointer into a non-const

  • 0

Const functions don’t seem to care if you dereference a member-pointer into a non-const reference. It’s also possible to return non-const member-pointers. However it’s not allowed to return a reference or an address of a data member that has a value type.

Why?

class Class
{
public:
unsigned int& value2(void) const
{
    *m_ptr = 10;
    return *m_ptr; 
    //Why?
}

unsigned int* value3(void) const
{
    return m_ptr; //Why?
}

private:
unsigned int* m_ptr;
};
  • 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-01T07:06:22+00:00Added an answer on June 1, 2026 at 7:06 am

    EDIT:

    Regarding your code: you’re never actually changing the member or returning a reference to it.

    unsigned int& value2(void) const
    {
        *m_ptr = 10;
        return *m_ptr; 
                    //Why?
    }
    //returns the value your member points to, not your member
    
    unsigned int* value3(void) const
    {
        return m_ptr; //Why?
    }
    //returns a copy of your member
    

    What would be disallowed would be the version I posted in my snippet.

    For example:

    struct A
    {
       int * x;
       int& getX() const { return *x; }
       int* getX1() const { return x; }
       int*& getX2() const { return x; } //error here
    };
    

    Only the last method will return an error, because:

    • first one returns the value, which is not const. The pointer is const.
    • second one returns the pointer by value, which again is ok
    • the third one actually returns the member by reference, that’s why it’s illegal
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Many functions in c take pointer to constant strings/chars as parameters eg void foo(const
I'm trying to redefine two member functions from their parent's definition. I don't know
Possible Duplicates: What's the use of const here Using 'const' in class's functions Hi
I've written tons of operator<<(std::ostream &, const T &) functions -- they're incredibly useful.
why do I get a discard qualifiers error: customExc.cpp: In member function ‘virtual const
Every time I write trivial getters (get functions that just return the value of
I have injected my DLL into a target application where I've hooked few WINAPI-functions
I'm thinking of using pure/const functions more heavily in my C++ code. ( pure/const
I have this two functions: procedure TDisplay.CubAssign(VAR Obj: TCubObj; CONST bReleaseOnExit: boolean); begin ReleaseCubOnExit:=
With explicitly deleted member functions in C++11, is it still worthwhile to inherit from

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.