Search code examples
c++smart-pointersmemory-addressscoped-ptr

How can I get the address of scoped_ptr?


I'm studying smart pointers, in particular scoped_ptr. I read about the operators * and ->. I tried to run this code:

int main(){
  boost::scoped_ptr<int>number(new int);
  *number = 432;
  std::cout<<"Value: "<<*number <<std::endl<< " Adress: "<< number <<std::endl;
  return 0;
}

And the result is:

Value: 432 Adress: 1

That isn't correct.

How have I to use -> operator to get the correct address?


Solution

  • Use get() member function:

    boost::scoped_ptr<int>number(new int);
    *number = 432;
    std::cout<<"Value: "<<*number <<std::endl<< " Adress: "<< number.get() <<std::endl;
    

    More details here