Why constness not enforced for pointers

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
6
down vote

favorite
1












Consider the following code snippet:



class A

public:

void nonConstFun()



;

class B

private:

A a_;
A * pA_;

public:

void fun() const

pA_->nonConstFun();
//a_.nonConstFun(); // Gives const related error

;

int main()

B b;
b.fun();



Here I am expecting the compiler to fail the compilation for lack of constness for calling A::nonConstFun() inside B::fun() irrespective of the type of A object.



However the compiler cribs for the object, but not for the pointer. Why?
I am using VS2017 on windows 10.










share|improve this question



























    up vote
    6
    down vote

    favorite
    1












    Consider the following code snippet:



    class A

    public:

    void nonConstFun()



    ;

    class B

    private:

    A a_;
    A * pA_;

    public:

    void fun() const

    pA_->nonConstFun();
    //a_.nonConstFun(); // Gives const related error

    ;

    int main()

    B b;
    b.fun();



    Here I am expecting the compiler to fail the compilation for lack of constness for calling A::nonConstFun() inside B::fun() irrespective of the type of A object.



    However the compiler cribs for the object, but not for the pointer. Why?
    I am using VS2017 on windows 10.










    share|improve this question

























      up vote
      6
      down vote

      favorite
      1









      up vote
      6
      down vote

      favorite
      1






      1





      Consider the following code snippet:



      class A

      public:

      void nonConstFun()



      ;

      class B

      private:

      A a_;
      A * pA_;

      public:

      void fun() const

      pA_->nonConstFun();
      //a_.nonConstFun(); // Gives const related error

      ;

      int main()

      B b;
      b.fun();



      Here I am expecting the compiler to fail the compilation for lack of constness for calling A::nonConstFun() inside B::fun() irrespective of the type of A object.



      However the compiler cribs for the object, but not for the pointer. Why?
      I am using VS2017 on windows 10.










      share|improve this question















      Consider the following code snippet:



      class A

      public:

      void nonConstFun()



      ;

      class B

      private:

      A a_;
      A * pA_;

      public:

      void fun() const

      pA_->nonConstFun();
      //a_.nonConstFun(); // Gives const related error

      ;

      int main()

      B b;
      b.fun();



      Here I am expecting the compiler to fail the compilation for lack of constness for calling A::nonConstFun() inside B::fun() irrespective of the type of A object.



      However the compiler cribs for the object, but not for the pointer. Why?
      I am using VS2017 on windows 10.







      c++ pointers const






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 26 mins ago









      Jarod42

      108k1298172




      108k1298172










      asked 27 mins ago









      Arun

      1,13121430




      1,13121430






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          8
          down vote



          accepted










          It is enforced.



          If you try changing the pointer, the compiler will not let you.



          The thing that the pointer points to, however, is a different conversation.



          Remember, T* const and T const* are not the same thing!



          You can protect that by either actually making it A const*, or simply by writing your function in the manner that is appropriate.






          share|improve this answer
















          • 2




            I think the question is why doesn't the function's const qualifier protect pointed to objects just as much as the pointer itself. Unfortunately if you make A const* then non const functions can't modify the object. This is a known "flaw/feature" of C++. Although the "why" of it is probably too "opinion" based for SO.
            – Galik
            13 mins ago






          • 2




            @Galik the why is explained by T* const and T const* are not the same thing!. As for the "flaw" - it's not a flaw. It's up to the programmer to decide if the object pointed by the pointer is logically inside the object B or if it logically external, because physically it is external. Modifying the *pa doesn't modify the object B in any physical way. The shortcoming of C++ is that it doesn't offer an easy way to represent a logicial belonging, which is in the process of fixing with propagate_const.
            – bolov
            8 mins ago











          • @bolov I think your comment to me here is the closest we have so far to actually answering the question. I already upvoted your answer for the new information it provided but it might be worth copying some of the substance (the design trade-offs) from your comment to me into your answer?
            – Galik
            2 mins ago


















          up vote
          8
          down vote













          The other answers explain what is happening. I want to add that what you want is propagate_const which is not standard yet, just experimental:




          std::experimental::propagate_const is a const-propagating wrapper for
          pointers and pointer-like objects. It treats the wrapped pointer as a
          pointer to const when accessed through a const access path, hence the
          name.




          struct B

          A a_;
          std::experimental::propagate_const<A *> pA_;

          void fun()

          pA_->nonConstFun(); // OK

          void fun() const

          // pA_->nonConstFun(); // compilation error

          ;





          share|improve this answer




















            Your Answer





            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52607625%2fwhy-constness-not-enforced-for-pointers%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            8
            down vote



            accepted










            It is enforced.



            If you try changing the pointer, the compiler will not let you.



            The thing that the pointer points to, however, is a different conversation.



            Remember, T* const and T const* are not the same thing!



            You can protect that by either actually making it A const*, or simply by writing your function in the manner that is appropriate.






            share|improve this answer
















            • 2




              I think the question is why doesn't the function's const qualifier protect pointed to objects just as much as the pointer itself. Unfortunately if you make A const* then non const functions can't modify the object. This is a known "flaw/feature" of C++. Although the "why" of it is probably too "opinion" based for SO.
              – Galik
              13 mins ago






            • 2




              @Galik the why is explained by T* const and T const* are not the same thing!. As for the "flaw" - it's not a flaw. It's up to the programmer to decide if the object pointed by the pointer is logically inside the object B or if it logically external, because physically it is external. Modifying the *pa doesn't modify the object B in any physical way. The shortcoming of C++ is that it doesn't offer an easy way to represent a logicial belonging, which is in the process of fixing with propagate_const.
              – bolov
              8 mins ago











            • @bolov I think your comment to me here is the closest we have so far to actually answering the question. I already upvoted your answer for the new information it provided but it might be worth copying some of the substance (the design trade-offs) from your comment to me into your answer?
              – Galik
              2 mins ago















            up vote
            8
            down vote



            accepted










            It is enforced.



            If you try changing the pointer, the compiler will not let you.



            The thing that the pointer points to, however, is a different conversation.



            Remember, T* const and T const* are not the same thing!



            You can protect that by either actually making it A const*, or simply by writing your function in the manner that is appropriate.






            share|improve this answer
















            • 2




              I think the question is why doesn't the function's const qualifier protect pointed to objects just as much as the pointer itself. Unfortunately if you make A const* then non const functions can't modify the object. This is a known "flaw/feature" of C++. Although the "why" of it is probably too "opinion" based for SO.
              – Galik
              13 mins ago






            • 2




              @Galik the why is explained by T* const and T const* are not the same thing!. As for the "flaw" - it's not a flaw. It's up to the programmer to decide if the object pointed by the pointer is logically inside the object B or if it logically external, because physically it is external. Modifying the *pa doesn't modify the object B in any physical way. The shortcoming of C++ is that it doesn't offer an easy way to represent a logicial belonging, which is in the process of fixing with propagate_const.
              – bolov
              8 mins ago











            • @bolov I think your comment to me here is the closest we have so far to actually answering the question. I already upvoted your answer for the new information it provided but it might be worth copying some of the substance (the design trade-offs) from your comment to me into your answer?
              – Galik
              2 mins ago













            up vote
            8
            down vote



            accepted







            up vote
            8
            down vote



            accepted






            It is enforced.



            If you try changing the pointer, the compiler will not let you.



            The thing that the pointer points to, however, is a different conversation.



            Remember, T* const and T const* are not the same thing!



            You can protect that by either actually making it A const*, or simply by writing your function in the manner that is appropriate.






            share|improve this answer












            It is enforced.



            If you try changing the pointer, the compiler will not let you.



            The thing that the pointer points to, however, is a different conversation.



            Remember, T* const and T const* are not the same thing!



            You can protect that by either actually making it A const*, or simply by writing your function in the manner that is appropriate.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 22 mins ago









            Lightness Races in Orbit

            272k48436747




            272k48436747







            • 2




              I think the question is why doesn't the function's const qualifier protect pointed to objects just as much as the pointer itself. Unfortunately if you make A const* then non const functions can't modify the object. This is a known "flaw/feature" of C++. Although the "why" of it is probably too "opinion" based for SO.
              – Galik
              13 mins ago






            • 2




              @Galik the why is explained by T* const and T const* are not the same thing!. As for the "flaw" - it's not a flaw. It's up to the programmer to decide if the object pointed by the pointer is logically inside the object B or if it logically external, because physically it is external. Modifying the *pa doesn't modify the object B in any physical way. The shortcoming of C++ is that it doesn't offer an easy way to represent a logicial belonging, which is in the process of fixing with propagate_const.
              – bolov
              8 mins ago











            • @bolov I think your comment to me here is the closest we have so far to actually answering the question. I already upvoted your answer for the new information it provided but it might be worth copying some of the substance (the design trade-offs) from your comment to me into your answer?
              – Galik
              2 mins ago













            • 2




              I think the question is why doesn't the function's const qualifier protect pointed to objects just as much as the pointer itself. Unfortunately if you make A const* then non const functions can't modify the object. This is a known "flaw/feature" of C++. Although the "why" of it is probably too "opinion" based for SO.
              – Galik
              13 mins ago






            • 2




              @Galik the why is explained by T* const and T const* are not the same thing!. As for the "flaw" - it's not a flaw. It's up to the programmer to decide if the object pointed by the pointer is logically inside the object B or if it logically external, because physically it is external. Modifying the *pa doesn't modify the object B in any physical way. The shortcoming of C++ is that it doesn't offer an easy way to represent a logicial belonging, which is in the process of fixing with propagate_const.
              – bolov
              8 mins ago











            • @bolov I think your comment to me here is the closest we have so far to actually answering the question. I already upvoted your answer for the new information it provided but it might be worth copying some of the substance (the design trade-offs) from your comment to me into your answer?
              – Galik
              2 mins ago








            2




            2




            I think the question is why doesn't the function's const qualifier protect pointed to objects just as much as the pointer itself. Unfortunately if you make A const* then non const functions can't modify the object. This is a known "flaw/feature" of C++. Although the "why" of it is probably too "opinion" based for SO.
            – Galik
            13 mins ago




            I think the question is why doesn't the function's const qualifier protect pointed to objects just as much as the pointer itself. Unfortunately if you make A const* then non const functions can't modify the object. This is a known "flaw/feature" of C++. Although the "why" of it is probably too "opinion" based for SO.
            – Galik
            13 mins ago




            2




            2




            @Galik the why is explained by T* const and T const* are not the same thing!. As for the "flaw" - it's not a flaw. It's up to the programmer to decide if the object pointed by the pointer is logically inside the object B or if it logically external, because physically it is external. Modifying the *pa doesn't modify the object B in any physical way. The shortcoming of C++ is that it doesn't offer an easy way to represent a logicial belonging, which is in the process of fixing with propagate_const.
            – bolov
            8 mins ago





            @Galik the why is explained by T* const and T const* are not the same thing!. As for the "flaw" - it's not a flaw. It's up to the programmer to decide if the object pointed by the pointer is logically inside the object B or if it logically external, because physically it is external. Modifying the *pa doesn't modify the object B in any physical way. The shortcoming of C++ is that it doesn't offer an easy way to represent a logicial belonging, which is in the process of fixing with propagate_const.
            – bolov
            8 mins ago













            @bolov I think your comment to me here is the closest we have so far to actually answering the question. I already upvoted your answer for the new information it provided but it might be worth copying some of the substance (the design trade-offs) from your comment to me into your answer?
            – Galik
            2 mins ago





            @bolov I think your comment to me here is the closest we have so far to actually answering the question. I already upvoted your answer for the new information it provided but it might be worth copying some of the substance (the design trade-offs) from your comment to me into your answer?
            – Galik
            2 mins ago













            up vote
            8
            down vote













            The other answers explain what is happening. I want to add that what you want is propagate_const which is not standard yet, just experimental:




            std::experimental::propagate_const is a const-propagating wrapper for
            pointers and pointer-like objects. It treats the wrapped pointer as a
            pointer to const when accessed through a const access path, hence the
            name.




            struct B

            A a_;
            std::experimental::propagate_const<A *> pA_;

            void fun()

            pA_->nonConstFun(); // OK

            void fun() const

            // pA_->nonConstFun(); // compilation error

            ;





            share|improve this answer
























              up vote
              8
              down vote













              The other answers explain what is happening. I want to add that what you want is propagate_const which is not standard yet, just experimental:




              std::experimental::propagate_const is a const-propagating wrapper for
              pointers and pointer-like objects. It treats the wrapped pointer as a
              pointer to const when accessed through a const access path, hence the
              name.




              struct B

              A a_;
              std::experimental::propagate_const<A *> pA_;

              void fun()

              pA_->nonConstFun(); // OK

              void fun() const

              // pA_->nonConstFun(); // compilation error

              ;





              share|improve this answer






















                up vote
                8
                down vote










                up vote
                8
                down vote









                The other answers explain what is happening. I want to add that what you want is propagate_const which is not standard yet, just experimental:




                std::experimental::propagate_const is a const-propagating wrapper for
                pointers and pointer-like objects. It treats the wrapped pointer as a
                pointer to const when accessed through a const access path, hence the
                name.




                struct B

                A a_;
                std::experimental::propagate_const<A *> pA_;

                void fun()

                pA_->nonConstFun(); // OK

                void fun() const

                // pA_->nonConstFun(); // compilation error

                ;





                share|improve this answer












                The other answers explain what is happening. I want to add that what you want is propagate_const which is not standard yet, just experimental:




                std::experimental::propagate_const is a const-propagating wrapper for
                pointers and pointer-like objects. It treats the wrapped pointer as a
                pointer to const when accessed through a const access path, hence the
                name.




                struct B

                A a_;
                std::experimental::propagate_const<A *> pA_;

                void fun()

                pA_->nonConstFun(); // OK

                void fun() const

                // pA_->nonConstFun(); // compilation error

                ;






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 19 mins ago









                bolov

                28k664120




                28k664120



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52607625%2fwhy-constness-not-enforced-for-pointers%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    What does second last employer means? [closed]

                    List of Gilmore Girls characters

                    One-line joke