what does this mean? const _Ty *_Myptr

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • shuisheng

    what does this mean? const _Ty *_Myptr

    Dear All,

    I find in std::auto_ptr, the private member is define as

    template<class _Ty>
    class auto_ptr
    {
    ....
    private:
    const _Ty *_Myptr;
    }

    what is the meaning of const _Ty *_Myptr; in a class member
    decalration?

    I appreicate your kind help!

    Shuisheng

  • Michael Ashton

    #2
    Re: what does this mean? const _Ty *_Myptr

    On Dec 16, 11:51 am, "shuisheng" <shuishen...@ya hoo.comwrote:
    Dear All,
    >
    I find in std::auto_ptr, the private member is define as
    >
    template<class _Ty>
    class auto_ptr
    {
    ...
    private:
    const _Ty *_Myptr;
    >
    }what is the meaning of const _Ty *_Myptr; in a class member
    decalration?
    >
    I appreicate your kind help!
    >
    Shuisheng


    Comment

    • Salt_Peter

      #3
      Re: what does this mean? const _Ty *_Myptr


      shuisheng wrote:
      Dear All,
      >
      I find in std::auto_ptr, the private member is define as
      >
      template<class _Ty>
      class auto_ptr
      {
      ...
      private:
      const _Ty *_Myptr;
      }
      >
      what is the meaning of const _Ty *_Myptr; in a class member
      decalration?
      >
      The class above has a template parameter <class _Ty>, substitute that
      parameter with whatever type you are passing to the template and you'll
      get your answer. If you specify < int as auto_ptr's template
      parameter:
      std::auto_ptr< int ap_n(new int);
      .... then the member will be:
      const int* _Myptr;

      Its a very, very simple construct. No voodoo involved.
      You can confirm such doubts by simply writing something like this:

      #include <iostream>
      #include <ostream>
      #include <typeinfo>

      template< typename T >
      struct Test
      {
      T t;
      };

      int main()
      {
      Test< double dtest;
      std::cout << typeid(dtest.t) .name() << std::endl;
      Test< char ctest;
      std::cout << typeid(ctest.t) .name() << std::endl;
      }

      Although, what type T will be is obvious.

      Comment

      • shuisheng

        #4
        Re: what does this mean? const _Ty *_Myptr


        shuisheng wrote:
        Dear All,
        >
        I find in std::auto_ptr, the private member is define as
        >
        template<class _Ty>
        class auto_ptr
        {
        ...
        private:
        const _Ty *_Myptr;
        }
        >
        what is the meaning of const _Ty *_Myptr; in a class member
        decalration?
        >
        I appreicate your kind help!
        >
        Shuisheng
        To make my question more clear. I am thinking that the 'const'
        qualifier should not be here because in auto_ptr, such as reset, its
        content is changed. Where is my wrong?

        Thanks.

        Comment

        • Kai-Uwe Bux

          #5
          Re: what does this mean? const _Ty *_Myptr

          shuisheng wrote:
          >
          shuisheng wrote:
          >Dear All,
          >>
          >I find in std::auto_ptr, the private member is define as
          >>
          >template<cla ss _Ty>
          >class auto_ptr
          >{
          >...
          >private:
          > const _Ty *_Myptr;
          >}
          >>
          >what is the meaning of const _Ty *_Myptr; in a class member
          >decalration?
          >>
          >I appreicate your kind help!
          >>
          >Shuisheng
          >
          To make my question more clear. I am thinking that the 'const'
          qualifier should not be here because in auto_ptr, such as reset, its
          content is changed. Where is my wrong?
          Distinguish the types

          T const *

          and

          T * const



          Best

          Kai-Uwe Bux

          Comment

          • Salt_Peter

            #6
            Re: what does this mean? const _Ty *_Myptr


            shuisheng wrote:
            shuisheng wrote:
            Dear All,

            I find in std::auto_ptr, the private member is define as

            template<class _Ty>
            class auto_ptr
            {
            ...
            private:
            const _Ty *_Myptr;
            }

            what is the meaning of const _Ty *_Myptr; in a class member
            decalration?

            I appreicate your kind help!

            Shuisheng
            >
            To make my question more clear. I am thinking that the 'const'
            qualifier should not be here because in auto_ptr, such as reset, its
            content is changed. Where is my wrong?
            >
            Thanks.
            Its not a constant pointer, its a mutable pointer to a constant
            variable (see Kai-Uwe's Post).
            Its contents are not changed during a reset(). Its given ownership of
            an entirely new object (which deletes the old object, if any).
            Reseating the pointer is not prohibited.

            #include <iostream>
            #include <ostream>

            int main()
            {
            int n(5);
            const int* p_n(&n);
            // *p_n = 6; // error
            std::cout << *p_n << std::endl;

            int m(99);
            // *p_n = 100; // error
            p_n = &m; // reseating ptr , perfectly ok
            std::cout << *p_n << std::endl;
            }

            /*
            5
            99
            */

            Comment

            • Bo Persson

              #7
              Re: what does this mean? const _Ty *_Myptr

              Salt_Peter wrote:
              shuisheng wrote:
              >shuisheng wrote:
              >>Dear All,
              >>>
              >>I find in std::auto_ptr, the private member is define as
              >>>
              >>template<clas s _Ty>
              >>class auto_ptr
              >>{
              >>...
              >>private:
              >> const _Ty *_Myptr;
              >>}
              >>>
              >>what is the meaning of const _Ty *_Myptr; in a class member
              >>decalration ?
              >>>
              >>I appreicate your kind help!
              >>>
              >>Shuisheng
              >>
              >To make my question more clear. I am thinking that the 'const'
              >qualifier should not be here because in auto_ptr, such as reset,
              >its content is changed. Where is my wrong?
              >>
              >Thanks.
              >
              Its not a constant pointer, its a mutable pointer to a constant
              variable (see Kai-Uwe's Post).
              Its contents are not changed during a reset(). Its given ownership
              of an entirely new object (which deletes the old object, if any).
              Reseating the pointer is not prohibited.
              >
              It is kind of interesting though, that an auto_ptr implementation stores a
              pointer-to-const. We are still able to assign to the underlying element
              using auto_ptr::opera tor*().

              An implementation detail, I know, but still surprising.


              Bo Persson


              Comment

              • Salt_Peter

                #8
                Re: what does this mean? const _Ty *_Myptr


                Bo Persson wrote:
                Salt_Peter wrote:
                shuisheng wrote:
                shuisheng wrote:
                >Dear All,
                >>
                >I find in std::auto_ptr, the private member is define as
                >>
                >template<cla ss _Ty>
                >class auto_ptr
                >{
                >...
                >private:
                > const _Ty *_Myptr;
                >}
                >>
                >what is the meaning of const _Ty *_Myptr; in a class member
                >decalration?
                >>
                >I appreicate your kind help!
                >>
                >Shuisheng
                >
                To make my question more clear. I am thinking that the 'const'
                qualifier should not be here because in auto_ptr, such as reset,
                its content is changed. Where is my wrong?
                >
                Thanks.
                Its not a constant pointer, its a mutable pointer to a constant
                variable (see Kai-Uwe's Post).
                Its contents are not changed during a reset(). Its given ownership
                of an entirely new object (which deletes the old object, if any).
                Reseating the pointer is not prohibited.
                >
                It is kind of interesting though, that an auto_ptr implementation stores a
                pointer-to-const. We are still able to assign to the underlying element
                using auto_ptr::opera tor*().
                >
                An implementation detail, I know, but still surprising.
                >
                Ok, but most don't store a ptr_to_constant . What is really strange with
                auto_ptrs is that both the copy ctor and the assignment operator modify
                a non-constant rvalue which renders the resulting rvalue into a nullptr
                (transfer of ownership).

                template< class T >
                class auto_ptr {
                ...
                autoptr( auto_ptr< T >& copy ) { ... } // non-const copy is modified
                ...
                };

                And a reference can result in a transfer of ownership:

                #include <iostream>
                #include <ostream>
                #include <memory>

                void foo(std::auto_p tr< int >& r_ap)
                {
                std::auto_ptr< int p_local(r_ap);
                std::cout << *r_ap << std::endl; // error !!!
                }

                int main()
                {
                std::auto_ptr< int p_n(new int(5));
                foo(p_n);
                std::cout << *p_n << std::endl; // error !!!
                }

                Fortunately, a const ref to auto_ptr<Tsolve s that issue.

                With respect of its shortcomings (ie: not copyable and can't support
                arrays) its a far, far better alternative than new/delete where
                appropriate.

                Comment

                Working...