Last active
August 19, 2020 06:41
-
-
Save junaidrahim/566d8febc275125fde206f3d804df891 to your computer and use it in GitHub Desktop.
Unique Pointer Example 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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