user5600884 user5600884 - 1 year ago 35
C++ Question

Unique Pointers in Modern c++

I have searched throughout the web quite a bit and have found so many different examples of

implementation in modern c++. I have started to update all of my code starting with
into the
and etc. However, with the smart pointers I don't think I understand exactly what I am suppose to be doing.

With a standard pointer I have:

object *temp = new object();

Then with the new smart pointer I have:

unquie_ptr<object> temp(new object());

Is this not the correct way of implementation? If so, it seems to be more typing / work to make smart pointers when I have a simple cleanup function built at the end of my program?

Answer Source

You can use more performant std::make_unique and std::make_shared.

However, make_unique is not available in C++11, but is in C++14 and above


std::unique_ptr<object> = std::make_unique<object>();


auto p = std::make_unique<object>();

and similar things go for shared_ptr

In C++11, you can use your expression for unique_ptr.

unquie_ptr<object> temp(new object());
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download