What exactly is the value of a boost::scoped_ptr pointer?

I am trying to understand the syntaxis of boost::scoped_ptr. Let's define and write to standard output a scoped_ptr pointer as well as the value it points at:

#include <boost/scoped_ptr.hpp>
#include <iostream>

int main()
  boost::scoped_ptr<int> p{new int{1}};

  std::cout << p.get() << '\n'; // ok
  std::cout << p << '\n';       // error   

  std::cout << *p.get() << '\n'; // ok
  std::cout << *p << '\n';       // ok. *p==*p.get()

Question. Why is *p.get()==*p and yet std::cout << p << '\n'; won't compile?

I'm working on g++ -std=c++11 -O2 -Wall -pedantic.


Why do &p and p.get() produce different results?

&p is the address of the scoped_ptr object. p.get() is the address of the int object it points to.

Why is *p.get()==*p and yet std::cout << p << '\n'; won't compile?

The two are unrelated.

*p is defined to be the same as *p.get(): dereferencing a scoped pointer accesses the object it points to, and so does dereferencing a raw pointer to the same object.

cout << p doesn't compile because there's no overload of << for streaming scoped pointers.

I'm working on g++ -std=c++11 -O2 -Wall -pedantic

Then consider std::unique_ptr (introduced in C++11) rather than boost::scoped_ptr. It's more standard, and also movable.

Need Your Help

Time series data representation in MarkLogic

time-series marklogic historical-db

Time-series data such as historical stock prices are usually stored in an RDBMS.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.