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 ?