boost:shared_ptr cast problem

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

    #1

    boost:shared_ptr cast problem

    Hello,

    I've code like :
    =============== =============== =============
    class A{
    public :
    // create print content
    friend std::ostream& operator<< (std::ostream&
    os, const A& a);

    }

    typedef boost::shared_p tr<AAPtr; // Define A class smart pointer

    APtr aPtr1(new A());
    APtr aPtr2(new A());
    APtr aPtr3(new A());

    vector<APtrAVec ;
    AVec.push_back( aPtr1);
    AVec.push_back( aPtr2);
    AVec.push_back( aPtr3);

    std::copy(AVec. begin(),AVec.en d(),
    std::ostream_it erator<APtr>(st d::cout, "\n"));

    std::copy(AVec. begin(),AVec.en d(), std::ostream_it erator< share_ptr<A>
    >(std::cout, "\n"));
    =============== =============== =============

    The two copy methods only output the address of pointers, they could
    just print the class A defined print contents. And following code
    works :
    =============== =============== =============
    vector<APtr>::i terator i = AVec.begin();
    for(; i!= AVec.end(); ++i){
    cout << *static_cast<AP tr>(*i) << endl;
    }
    =============== =============== =============

    Anyone has some ideas for that ? Thank you in advance.


    Jun
  • Jun

    #2
    Re: boost:shared_pt r cast problem

    On Nov 25, 10:12 pm, Jun <junh...@gmail. comwrote:
    Hello,
    >
    I've code like :
    =============== =============== =============
    class A{
    public :
    // create print content
    friend std::ostream& operator<< (std::ostream&
    os, const A& a);
    >
    }
    >
    typedef boost::shared_p tr<AAPtr; // Define A class smart pointer
    >
    APtr aPtr1(new A());
    APtr aPtr2(new A());
    APtr aPtr3(new A());
    >
    vector<APtrAVec ;
    AVec.push_back( aPtr1);
    AVec.push_back( aPtr2);
    AVec.push_back( aPtr3);
    >
    std::copy(AVec. begin(),AVec.en d(),
    std::ostream_it erator<APtr>(st d::cout, "\n"));
    >
    std::copy(AVec. begin(),AVec.en d(), std::ostream_it erator< share_ptr<A>>(s td::cout, "\n"));
    >
    =============== =============== =============
    >
    The two copy methods only output the address of pointers, they could
    just print the class A defined print contents. And following code
    works :
    =============== =============== =============
    vector<APtr>::i terator i = AVec.begin();
    for(; i!= AVec.end(); ++i){
    cout << *static_cast<AP tr>(*i) << endl;
    }
    =============== =============== =============
    >
    Anyone has some ideas for that ? Thank you in advance.
    >
    Jun
    Actually, It's boost::shared_p tr serialization problem.

    Comment

    • Kai-Uwe Bux

      #3
      Re: boost:shared_pt r cast problem

      Jun wrote:
      Hello,
      >
      I've code like :
      =============== =============== =============
      class A{
      public :
      // create print content
      friend std::ostream& operator<< (std::ostream&
      os, const A& a);
      >
      }
      >
      typedef boost::shared_p tr<AAPtr; // Define A class smart pointer
      >
      APtr aPtr1(new A());
      APtr aPtr2(new A());
      APtr aPtr3(new A());
      >
      vector<APtrAVec ;
      AVec.push_back( aPtr1);
      AVec.push_back( aPtr2);
      AVec.push_back( aPtr3);
      >
      std::copy(AVec. begin(),AVec.en d(),
      std::ostream_it erator<APtr>(st d::cout, "\n"));
      >
      std::copy(AVec. begin(),AVec.en d(),
      std::ostream_it erator< share_ptr<A(std ::cout, "\n"));
      =============== =============== =============
      >
      The two copy methods only output the address of pointers, they could
      just print the class A defined print contents.
      Why would you expect that? Consider

      int i = 5;
      std::cout << i << std::endl
      << &i << std::endl;

      You would expect the second line to print an address, would you not? Why
      should shared_ptr<beha ve differently? Also note that shared_ptr could
      have null value. In that case, there is no pointee that could be printed
      instead of an address.

      And following code works :
      =============== =============== =============
      vector<APtr>::i terator i = AVec.begin();
      for(; i!= AVec.end(); ++i){
      cout << *static_cast<AP tr>(*i) << endl;
      }
      =============== =============== =============
      Anyone has some ideas for that ?
      Yes, leave out the cast: *i is already of type APtr.

      cout << *(*i) << endl;



      BTW: I have the feeling that I did not really understand what the real
      problem is that you want to solve. Maybe, you oversimplified it for the
      purpose of the post. Could you provide a little background as to _why_ you
      want to print the shared_ptr and _why_ you feel it would be the
      RightThing(tm) if that printed the pointee instead of an address? Maybe, it
      is the context of the underlying problem that makes you think printing the
      pointee would be right. In that case, it would be good to share the
      underlying problem with us, because it might have a well known solution.


      Best

      Kai-Uwe Bux

      Comment

      • Jun

        #4
        Re: boost:shared_pt r cast problem

        On Nov 26, 1:52 am, Kai-Uwe Bux <jkherci...@gmx .netwrote:
        Jun wrote:
        Hello,
        >
        I've code like :
        =============== =============== =============
        class A{
        public :
        // create print content
        friend std::ostream& operator<< (std::ostream&
        os, const A& a);
        >
        }
        >
        typedef boost::shared_p tr<AAPtr; // Define A class smart pointer
        >
        APtr aPtr1(new A());
        APtr aPtr2(new A());
        APtr aPtr3(new A());
        >
        vector<APtrAVec ;
        AVec.push_back( aPtr1);
        AVec.push_back( aPtr2);
        AVec.push_back( aPtr3);
        >
        std::copy(AVec. begin(),AVec.en d(),
        std::ostream_it erator<APtr>(st d::cout, "\n"));
        >
        std::copy(AVec. begin(),AVec.en d(),
        std::ostream_it erator< share_ptr<A(std ::cout, "\n"));
        =============== =============== =============
        >
        The two copy methods only output the address of pointers, they could
        just print the class A defined print contents.
        >
        Why would you expect that? Consider
        >
        int i = 5;
        std::cout << i << std::endl
        << &i << std::endl;
        >
        You would expect the second line to print an address, would you not? Why
        should shared_ptr<beha ve differently? Also note that shared_ptr could
        have null value. In that case, there is no pointee that could be printed
        instead of an address.
        >
        And following code works :
        =============== =============== =============
        vector<APtr>::i terator i = AVec.begin();
        for(; i!= AVec.end(); ++i){
        cout << *static_cast<AP tr>(*i) << endl;
        }
        =============== =============== =============
        Anyone has some ideas for that ?
        >
        Yes, leave out the cast: *i is already of type APtr.
        >
        cout << *(*i) << endl;
        >
        BTW: I have the feeling that I did not really understand what the real
        problem is that you want to solve. Maybe, you oversimplified it for the
        purpose of the post. Could you provide a little background as to _why_ you
        want to print the shared_ptr and _why_ you feel it would be the
        RightThing(tm) if that printed the pointee instead of an address? Maybe, it
        is the context of the underlying problem that makes you think printing the
        pointee would be right. In that case, it would be good to share the
        underlying problem with us, because it might have a well known solution.
        >
        Best
        >
        Kai-Uwe Bux
        I've a person class, which contains name, age. I applied output as
        name age,
        for my custom class. By storing it as a smart pointer PersonPtr in a
        vector.
        Then my idea is using copy algorithm to print out all the PersonPtr in
        the
        vector, but failed. Since PersonPtr has the serialization problem. By
        using
        copy algorithm, it only prints the address of smart pointer instead
        name age,
        which i defined.

        Comment

        • Kai-Uwe Bux

          #5
          Re: boost:shared_pt r cast problem

          Jun wrote:
          On Nov 26, 1:52 am, Kai-Uwe Bux <jkherci...@gmx .netwrote:
          >Jun wrote:
          Hello,
          >>
          I've code like :
          =============== =============== =============
          class A{
          public :
          // create print content
          friend std::ostream& operator<< (std::ostream&
          os, const A& a);
          >>
          }
          >>
          typedef boost::shared_p tr<AAPtr; // Define A class smart pointer
          >>
          APtr aPtr1(new A());
          APtr aPtr2(new A());
          APtr aPtr3(new A());
          >>
          vector<APtrAVec ;
          AVec.push_back( aPtr1);
          AVec.push_back( aPtr2);
          AVec.push_back( aPtr3);
          >>
          std::copy(AVec. begin(),AVec.en d(),
          std::ostream_it erator<APtr>(st d::cout, "\n"));
          >>
          std::copy(AVec. begin(),AVec.en d(),
          std::ostream_it erator< share_ptr<A(std ::cout, "\n"));
          =============== =============== =============
          >>
          The two copy methods only output the address of pointers, they could
          just print the class A defined print contents.
          >>
          >Why would you expect that? Consider
          >>
          > int i = 5;
          > std::cout << i << std::endl
          > << &i << std::endl;
          >>
          >You would expect the second line to print an address, would you not? Why
          >should shared_ptr<beha ve differently? Also note that shared_ptr could
          >have null value. In that case, there is no pointee that could be printed
          >instead of an address.
          >>
          And following code works :
          =============== =============== =============
          vector<APtr>::i terator i = AVec.begin();
          for(; i!= AVec.end(); ++i){
          cout << *static_cast<AP tr>(*i) << endl;
          }
          =============== =============== =============
          Anyone has some ideas for that ?
          >>
          >Yes, leave out the cast: *i is already of type APtr.
          >>
          > cout << *(*i) << endl;
          >>
          >BTW: I have the feeling that I did not really understand what the real
          >problem is that you want to solve. Maybe, you oversimplified it for the
          >purpose of the post. Could you provide a little background as to _why_
          >you want to print the shared_ptr and _why_ you feel it would be the
          >RightThing(t m) if that printed the pointee instead of an address? Maybe,
          >it is the context of the underlying problem that makes you think printing
          >the pointee would be right. In that case, it would be good to share the
          >underlying problem with us, because it might have a well known solution.
          >>
          >Best
          >>
          >Kai-Uwe Bux
          >
          I've a person class, which contains name, age. I applied output as
          name age,
          for my custom class. By storing it as a smart pointer PersonPtr in a
          vector.
          Then my idea is using copy algorithm to print out all the PersonPtr in
          the
          vector, but failed. Since PersonPtr has the serialization problem. By
          using
          copy algorithm, it only prints the address of smart pointer instead
          name age,
          which i defined.
          You might try:

          template < typename T >
          T const & deref ( std::tr1::share d_ptr<Tptr ) {
          return ( *ptr );
          }

          and

          std::transform( AVec.begin(), AVec.end(),
          std::ostream_it erator< A >( std::cout, "\n" ),
          &deref<A);

          Maybe, there is a way to use boost::lambda and just say

          std::transform( AVec.begin(), AVec.end(),
          std::ostream_it erator< A >( std::cout, "\n" ),
          *_1);

          without the need to define deref.


          Best

          Kai-Uwe Bux

          Comment

          • Jun

            #6
            Re: boost:shared_pt r cast problem

            On Nov 26, 1:10 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
            Jun wrote:
            On Nov 26, 1:52 am, Kai-Uwe Bux <jkherci...@gmx .netwrote:
            Jun wrote:
            Hello,
            >
            I've code like :
            =============== =============== =============
            class A{
            public :
            // create print content
            friend std::ostream& operator<< (std::ostream&
            os, const A& a);
            >
            }
            >
            typedef boost::shared_p tr<AAPtr; // Define A class smart pointer
            >
            APtr aPtr1(new A());
            APtr aPtr2(new A());
            APtr aPtr3(new A());
            >
            vector<APtrAVec ;
            AVec.push_back( aPtr1);
            AVec.push_back( aPtr2);
            AVec.push_back( aPtr3);
            >
            std::copy(AVec. begin(),AVec.en d(),
            std::ostream_it erator<APtr>(st d::cout, "\n"));
            >
            std::copy(AVec. begin(),AVec.en d(),
            std::ostream_it erator< share_ptr<A(std ::cout, "\n"));
            =============== =============== =============
            >
            The two copy methods only output the address of pointers, they could
            just print the class A defined print contents.
            >
            Why would you expect that? Consider
            >
            int i = 5;
            std::cout << i << std::endl
            << &i << std::endl;
            >
            You would expect the second line to print an address, would you not? Why
            should shared_ptr<beha ve differently? Also note that shared_ptr could
            have null value. In that case, there is no pointee that could be printed
            instead of an address.
            >
            And following code works :
            =============== =============== =============
            vector<APtr>::i terator i = AVec.begin();
            for(; i!= AVec.end(); ++i){
            cout << *static_cast<AP tr>(*i) << endl;
            }
            =============== =============== =============
            Anyone has some ideas for that ?
            >
            Yes, leave out the cast: *i is already of type APtr.
            >
            cout << *(*i) << endl;
            >
            BTW: I have the feeling that I did not really understand what the real
            problem is that you want to solve. Maybe, you oversimplified it for the
            purpose of the post. Could you provide a little background as to _why_
            you want to print the shared_ptr and _why_ you feel it would be the
            RightThing(tm) if that printed the pointee instead of an address? Maybe,
            it is the context of the underlying problem that makes you think printing
            the pointee would be right. In that case, it would be good to share the
            underlying problem with us, because it might have a well known solution.
            >
            Best
            >
            Kai-Uwe Bux
            >
            I've a person class, which contains name, age. I applied output as
            name age,
            for my custom class. By storing it as a smart pointer PersonPtr in a
            vector.
            Then my idea is using copy algorithm to print out all the PersonPtr in
            the
            vector, but failed. Since PersonPtr has the serialization problem. By
            using
            copy algorithm, it only prints the address of smart pointer instead
            name age,
            which i defined.
            >
            You might try:
            >
            template < typename T >
            T const & deref ( std::tr1::share d_ptr<Tptr ) {
            return ( *ptr );
            >
            }
            >
            and
            >
            std::transform( AVec.begin(), AVec.end(),
            std::ostream_it erator< A >( std::cout, "\n" ),
            &deref<A);
            >
            Maybe, there is a way to use boost::lambda and just say
            >
            std::transform( AVec.begin(), AVec.end(),
            std::ostream_it erator< A >( std::cout, "\n" ),
            *_1);
            >
            without the need to define deref.
            >
            Best
            >
            Kai-Uwe Bux
            I saw the lambda solution, anyway, I will try and post the results.
            Jun

            Comment

            Working...