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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:21:20+00:00 2026-06-11T10:21:20+00:00

I have a member function which is declared const and modifies data via a

  • 0

I have a member function which is declared const and modifies data via a pointer. This seems misleading. Should I remove the const keyword?

I would like to know how others handle this situation in their code. Do people just add a comment to clarify what is going on? Do they not add the const keyword to the member function? Maybe something else completely?

Any advice is appreciated.

  • 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-11T10:21:22+00:00Added an answer on June 11, 2026 at 10:21 am

    You have essentially two choices:

    Deep constness:

    class Foo
    {
        T * ptr;
    public:
        T       & operator*()       { return *ptr; }
        T const & operator*() const { return *ptr; }
        T       * operator->()       { return ptr; }
        T const * operator->() const { return ptr; }
    };
    

    Shallow constness:

    class Foo
    {
        T * ptr;
    public:
        T & operator*() const { return *ptr; }
        T * operator->() const { return ptr; }
    };
    

    It’s really up to you, and to the purpose of your class. If the class is a smart pointer, it would seem reasonable to have shallow constness semantics, since the class is supposed to be as similar to a raw pointer as possible (and you can of course have a constant raw pointer to a non-constant pointee).

    Otherwise, you should ask yourself why you would be exposing access to a member pointer object at all. It’s certainly possible that you want to give mutable access via constant references to your class, but I imagine those are special and rare circumstances. There shouldn’t really be that many raw pointers in your code in the first place. Returning a deeply-const reference by dereferencing a pointer should be fine, but usually in better encapsulated “getter” functions which hide the fact that there is a pointer inside your class, like T const & get() const { return *ptr; }.

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

Sidebar

Related Questions

I have a std::atomic which contains a pointer to a member function. Everything compiles
I have next class and try to declare member function which will return pointer
Imagine I have a class Window with a member function show which causes the
I have a class ( EAGLView ) which calls a member function of a
I have an inline member function defined under class MyClass int MyClass::myInlineFunction(); This function
I have a class, in which i declare a static function. I register this
I have a problem with class member function templates. It seems that gcc is
If I have a member variable such as this (declared in the body of
i have two data classes which hold only data members(no functions). One is CallTask
I have a member function in a class that has a callback, but the

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.