Skip to content

Instantly share code, notes, and snippets.

@junaidrahim
Last active August 19, 2020 06:41
Show Gist options
  • Save junaidrahim/566d8febc275125fde206f3d804df891 to your computer and use it in GitHub Desktop.
Save junaidrahim/566d8febc275125fde206f3d804df891 to your computer and use it in GitHub Desktop.
Unique Pointer Example 3
#include <iostream>
#include <memory>
using namespace std;
class SomeBigObject{
public:
float data[1000];
void something() {
// just something
}
void someOtherThing(){
// just some other thing
}
};
void ProcessBigObject(const SomeBigObject& o){
// do some crazy processing with o
}
int main(){
// create a unique pointer
// unique_ptr<SomeBigObject> pBigObject(new SomeBigObject()); // you can also do it this way
unique_ptr<SomeBigObject> pBigObject = make_unique<SomeBigObject>(); // but this is preferred
// use it like just any other pointer
pBigObject->something();
pBigObject->someOtherThing();
ProcessBigObject(*pBigObject); // pass it as a reference
// pBigObject is automatically freed when the main function block ends
// no need to call delete explicitly
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment