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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:11:30+00:00 2026-05-22T02:11:30+00:00

I know this has been asked a lot, but the only answers I could

  • 0

I know this has been asked a lot, but the only answers I could find was when the const-ness was actually casted away using (int*) or similar. Why isn’t the const qualifier working on pointer type member variables on const objects when no cast is involved?

#include <iostream>

class bar {
public:
    void doit()       { std::cout << "    bar::doit() non-const\n"; }
    void doit() const { std::cout << "    bar::doit() const\n"; }
};

class foo {
    bar* mybar1;
    bar mybar2;
public:
    foo() : mybar1(new bar) {}
    void doit() const {
        std::cout << "foo::doit() const\n";
        std::cout << "  calling mybar1->doit()\n";
        mybar1->doit();  // This calls bar::doit() instead of bar::doit() const
        std::cout << "  calling mybar2.doit()\n";
        mybar2.doit(); // This calls bar::doit() const correctly
    }
    // ... (proper copying elided for brevity)
};

int main(void)
{
    const foo foobar;  // NOTE: foobar is const
    foobar.doit();
}

The code above yields the following output (tested in gcc 4.5.2 and vc100):

foo::doit() const
  calling mybar1->doit()
    bar::doit() non-const         <-- Why ?
  calling mybar2.doit()
    bar::doit() const
  • 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-22T02:11:31+00:00Added an answer on May 22, 2026 at 2:11 am

    When a foo instance is const, its data members are const too, but this applies differently for pointers than you might at first think:

    struct A {
      int *p;
    };
    
    A const obj;
    

    The type of obj.p is int * const, not int const *; that is, a constant pointer to int, not a pointer to constant int.

    For another way to look at it, let’s start with a function:

    template<class T>
    T const& const_(T const &x) {
      return x;
    }
    

    Now imagine we have an A instance, and we make it const. You can imagine that as applying const_ on each data member.

    A nc;
    // nc.p has type int*.
    typedef int *T;  // T is the type of nc.p.
    
    T const &p_when_nc_is_const = const_(nc.p);
    // "T const" is "int * const".
    
    const T &be_wary_of_where_you_place_const = const_(nc.p);
    // "const T" is "int * const".
    // "const T" is *not* "const int *".
    

    The variable be_wary_of_where_you_place_const shows that “adding const” is not the same as prepending “const” to the literal text of a type.

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

Sidebar

Related Questions

I know this question has been asked a lot, but I want to find
Yes, I know this question has been asked a lot of times, but I
I know this has been asked thousands of times but I just can't find
I know this has been asked many times but i'm yet to find a
I know this has been asked before but all the answers I found didn't
I know this has been asked much the same before but no answers seem
I know this type of question has been asked a lot but none of
I know this has been asked a lot, but I have a server-side custom
I know this has probably been asked a lot on here, but I am
I know this has been asked a lot of times, but I have honestly

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.