Back
Close

Computing with Data

elgeish
33.2K views

Smart Pointers - Part II

#include <memory>
using namespace std;
unique_ptr<int> create_unique_pointer() {
return unique_ptr<int>(new int(4));
}
int main() {
unique_ptr<int> pi1(new int(3));
// error: two unique_ptr pointers pointing to the same memory
unique_ptr<int> pi2 = pi1;
// ok: new unique_ptr takes over as old unique_ptr is destroyed
unique_ptr<int> pi3 = create_unique_pointer();
return 0;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants