#include <iostream>
#include <memory>
using namespace std;
int main() {
std::unique_ptr<int> ptrA = std::make_unique<int>(10);
ptrA = std::make_unique<int>(20); // case I
return 0;
}
#include <iostream>
#include <memory>
using namespace std;
int main() {
std::unique_ptr<int> ptrA = std::make_unique<int>(10);
ptrA = nullptr; // case II or ptrA.reset()
ptrA = std::make_unique<int>(20);
return 0;
}
I have seen many people use Case II
. However, std::unique_ptr
is a smart pointer, I don't think we should assign either nullptr
or call reset
before reassigning a new value to it.
Please correct me if I am wrong here.
Assigning nullptr
before assigning a new value is pointless.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With