The std::unique_ptr has unique ownership of the object it manages. That means that more than one std::unique_ptr object can not contain the managed object pointer. That’s why we can not assign a std::unique_ptr to another one. For the same reason, we can not pass std::unique_ptr as an argument of a function. But we can always … Continue reading “Move std::unique_ptr to Pass to a Function or Assign to Another Variable”
Tag: C++
Custom Deleter in std::unique_ptr
In my previous article, we discussed how std::unique_ptr helps to avoid memory leak. By default it calls the default deleter (C++ delete operator) when the std::unique_ptr goes out of context. But many times default deleter is not good enough for the type of pointer we deal with. For example, if you work with a file … Continue reading “Custom Deleter in std::unique_ptr”
Static Members in C++ Template Class
We generally use static members in C++ class to maintain something that is not object specific, rather class specific. For example, if we want to maintain a counter about how many objects are created from a class, we keep a static counter in the class. In the class constructor, we increment the counter and we … Continue reading “Static Members in C++ Template Class”
What is std::unique_ptr? How to Use std::unique_ptr?
std::unique_ptr is a smart pointer that owns and manages another object through pointer. std::unique_ptr has unique ownership of the object. That means only one std::unique_ptr can have the ownership of the actual object. We can not assign a std::unique_ptr object to another smart pointer. std::unique_ptr disposes the managed object when the unique_ptr goes out of … Continue reading “What is std::unique_ptr? How to Use std::unique_ptr?”
How to do Asynchronous Operations using boost::asio?
Boost::asio is primarily designed for accomplishing time-consuming networking I/O (Input/Output) operations in an asynchronous way. I/O operations over network usually takes considerable time to complete. In synchronous mode, if you call a function that does I/O operation over network, the calling thread blocks until the I/O operation is complete. This is not acceptable in many … Continue reading “How to do Asynchronous Operations using boost::asio?”