Like Tree2Likes
  • 1 Post By laserlight
  • 1 Post By laserlight

couple of questions concerning Boost::scoped_ptr

This is a discussion on couple of questions concerning Boost::scoped_ptr within the C++ Programming forums, part of the General Programming Boards category; hello all , today i was reading a boost tutorial and there i faced scoped_ptr and the likes . i ...

  1. #1
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493

    couple of questions concerning Boost::scoped_ptr

    hello all , today i was reading a boost tutorial and there i faced scoped_ptr and the likes . i faced some issues which i would like to discuss with you .
    first of all consider this little bit of code:
    Code:
    #include <boost/scoped_ptr.hpp>
    
    int main()
    {
        boost::scoped_ptr<int> i(new int);
        *i = 1;
        *i.get() = 2;
        std::cout<<"?= "<<i<<" The Address= "<< i.get() <<"\t The Value= "<<*i.get();
        i.reset(new int);
    }
    and the output is (e.g):
    Code:
    ?=        The Address=0x311018  The Value=2
    First question:
    Why is the out of i empty? is it not supposed to print out the address of the object which i is referring to ?
    Second question :
    What exactly get() method do ? is it s getter? or a setter ?
    if it gets sth where does it put it ?
    i tried the following code :
    Code:
    std::cout<<"\t OutPut5= "<<(*i.get()=5) <<"\t OutPut2= "<<*i.get();
    output:
    Code:
     OutPut5= 5     OutPut2= 2
    what is happening here? what happened to that 5?
    Third question :
    Why is it said that we can not use scoped_ptr with arrays ?
    why exactly did they divided this pointer class into two separate classes ?
    well couldn't they just use an if statement to find out what we are dealing with at runtime ? and if it were an array use that delete[] operator ?
    aside form that what is the crucial difference between delete and delete[] ?

    and my forth question :
    what is the operator bool() ? concerning these pointers ? whats the use of it ? and how to use it ?
    Last edited by Masterx; 04-08-2012 at 11:53 AM.
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,992
    Quote Originally Posted by Masterx
    Why is the out of i empty? is it not supposed to print out the address of the object which i is referring to ?
    Looking at the documentation, operator<< is not overloaded, so what you are getting is the result of using the smart pointer in an unintended fashion. Effectively, it is "undefined behaviour" with respect to the library.

    Quote Originally Posted by Masterx
    What exactly get() method do ?
    It returns the internal pointer.

    Quote Originally Posted by Masterx
    is it s getter? or a setter ?
    It is a getter.

    Quote Originally Posted by Masterx
    if it gets sth where does it put it ?
    What do you mean? It just returns a value.

    Quote Originally Posted by Masterx
    what is happening here? what happened to that 5?
    The order of evaluation of function arguments is unspecified, so you should not be doing this in the first place.

    Quote Originally Posted by Masterx
    Why is it said that we can not use scoped_ptr with arrays ?
    Because the deleter is not for arrays.

    Quote Originally Posted by Masterx
    well couldn't they just use an if statement to find out what we are dealing with at runtime ? and if it were an array use that delete[] operator ?
    No, though the approach used by std::unique_ptr is to allow for a custom deleter to be specified, and then specialise this for array types.

    Quote Originally Posted by Masterx
    aside form that what is the crucial difference between delete and delete[] ?
    One is for lone objects and the other for arrays.
    Masterx likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    Thanks laiserlight .
    would you answer my forth question ?
    and my forth question :
    what is the operator bool() ? concerning these pointers ? whats the use of it ? and how to use it ?
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,992
    It is so that you can use it like:
    Code:
    if (p)
    Masterx likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    can somepne expalin to me , what this notation is for ? and what it does? and what is its other equivalents?
    Code:
    boost::shared_ptr<int> *sh = static_cast<boost::shared_ptr<int>*>(p);
    it really looks weird to me , never seen it anywhere
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,992
    What notation? The static_cast?

    Anyway, it seems rather unnecessary to have a pointer to a shared_ptr.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Couple of questions
    By kzar in forum C++ Programming
    Replies: 13
    Last Post: 10-08-2005, 01:13 PM
  2. couple questions...
    By seal in forum C Programming
    Replies: 8
    Last Post: 08-29-2005, 07:14 PM
  3. Couple Questions...
    By GameGenie in forum C++ Programming
    Replies: 4
    Last Post: 08-26-2005, 02:57 PM
  4. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM
  5. Couple Questions
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 11-04-2001, 04:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21