B C
B C

Reputation: 3

What is the difference between these two uses of unique_ptr?

Class "a" contains attributes: service_ of type boost::asio_ioservice and sock_, a unique_ptr of type boost::asio::ip::tcp::socket.

The following constructor code exists with sock_ properly pointing at a new socket object.

a::a() : service_(), sock_(new boost::asio::ip::tcp::socket(service_))
{
}

The following constructor code does not. The debugger lists sock_ as "empty".

a::a() : service_(), sock_(nullptr)
{
     sock_(new boost::asio::ip::tcp::socket(service_));
}

Why?

Upvotes: 0

Views: 165

Answers (1)

sehe
sehe

Reputation: 393759

As a statement:

sock_(new boost::asio::ip::tcp::socket(service_));

is simply not valid c++. Did you think of

sock_.reset(new boost::asio::ip::tcp::socket(service_));

instead?

Or

sock_ = std::make_unique<boost::asio::ip::tcp::socket>(service_);

Upvotes: 2

Related Questions