cpp_unique_pointer - 8BitsCoding/RobotMentor GitHub Wiki
- ๋ ์ด์ ํฌ์ธํฐ๊ฐ ํ์ํ์ง ์์ ๋ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํด์ ํด์ผํ๋ค.
๊ฐ๋น์ง ์ปฌ๋ ํฐ ๋ณด๋ค ๋น ๋ฅด๊ณ ์ง์ ๋ฉ๋ชจ๋ฆฌ ํด์ ๋ฅผ ํ๊ณ ์ถ์ง ์๋ค -> ์ค๋งํธ ํฌ์ธํฐ
#include <memory>
#include "Vector.h"
int main() {
std::unique_ptr<Vector> myVector(new Vector(10.f, 30.f));
// ํฌ์ธํฐ ๋ถํธ(*)๊ฐ ์์
myVector->Print();
// ํฌ์ธํฐ ์ฒ๋ผ ์ฌ์ฉ
// delete๊ฐ ์์
return 0;
}
- ํฌ์ธํฐ(๊ธฐ์กด์ ์ฐ๋)๋ฅผ ๋จ๋ ์ผ๋ก ์์ ํ๋ค.
- ์์ ํฌ์ธํฐ๋ ๋๊ตฌํ๊ณ ๋ ๊ณต์ ๋์ง ์๋๋ค.
- ๋ณต์ฌ๋ ๋์ ์ด ๋ถ๊ฐ๋ฅ!
- unique_ptr์ด ๋ฒ์๋ฅผ ๋ฒ์ด๋๋ค๋ฉด ์์ ํฌ์ธํฐ๋ ์ง์(delete)์ง๋ค.
std::unique_ptr<Vector> myVector(new Vector(10.f, 30.f));
std::unique_ptr<Vector> copiedVector1 = myVector; // Error
std::unique_ptr<Vector> copiedVector2(myVector); // Error
class Player
{
private:
Vector* mLocation;
};
Player::Player(std::string name) :
mLocation(new Vector())
{
}
Player::~Player(){
// ๋งค๋ฒ ์ ์ด์ค์ผํจ.
delete mLocation;
}
class Player
{
private:
std::unique<Vector> mLocation;
};
Player::Player(std::string name) :
mLocation(new Vector())
{
}
// delete์๋ต ๊ฐ๋ฅ!
int main() {
Vector* vector = new Vector(10.f, 30.f);
delete vector;
return 0;
}
int main() {
std::unique_ptr<Vector> myVector(new Vector(10.f, 30.f));
return 0;
}
#include <vector>
int main() {
std::vector<Player*> players;
players.push_back(new Player("Lulu"));
players.push_back(new Player("Coco"));
for(int i = 0; i < players.size(); ++i)
{
delete players[i];
}
players.clear();
}
#include <vector>
int main() {
std::vector<std::unique_ptr<Player>> playerList;
playerList.push_back(std::unique_ptr<Player>(new Player("Lulu")));
playerList.push_back(std::unique_ptr<Player>(new Player("Coco")));
playerList.clear();
}
์ ์ธํ ํฌ์ธํฐ๋ฅผ resetํ๊ณ ์ถ๋ค
int main() {
std::unique_ptr<Vector> vector = std::make_unique<Vector>(10.f, 30.f);
vector.reset(new Vector(20.f, 40.f));
vector.reset(); // nullptr
}
nullptr์ด ๊ฐ๋ ์ฑ์ด ๋๊ธฐ์
vector = nullptr
์ธ ๊ฒ.
void Vector::Add(const Vector* other)
{
//
}
// main
std::unique_ptr<Vector> vector = std::make_unique<Vector>(10.f, 30.f);
std::unique_ptr<Vector> anotherVector = std::make_unique<Vector>(20.f, 40.f);
vector->Add(anotherVector.get());
// main
std::unique_ptr<Vector> vector = std::make_unique<Vector>(10.f, 30.f);
Vector* vectorPtr = vector.release();
์ฌ์ค ์ธ ์ผ์ด ๊ทธ๋ฆฌ ์์!
#include <memory>
#include "Vector.h"
int main() {
std::unique_ptr<Vector> vector = std::make_unique<Vecor>(10.f, 30.f);
std::unique_ptr<Vector> anotherVector(std::move(vector));
}
- ๊ฐ์ฒด A์ ๋ชจ๋ ๋ฉค๋ฒ๋ฅผ ํฌ๊ธฐํ๊ณ ๊ทธ ์์ ๊ถ์ B์๊ฒ ์ฃผ๋ ๋ฐฉ๋ฒ
- ๋ฉ๋ชจ๋ฆฌ ํ ๋น๊ณผ ํด์ ๊ฐ ์ผ์ด๋์ง๋ ์๋๋ค.
- ๊ฐ๋จํ๊ฒ, A์ ์๋ ๋ชจ๋ ํฌ์ธํฐ๋ฅผ B์ ๋์ ํ๊ณ A์๋ nullptr์ ๋ฃ๋๋ค๊ณ ์๊ฐ
- r-value์ move์์ฑ์๋ฅผ ๋ฐฐ์ฐ๋ฉด ์ด๋ป๊ฒ ๋์ํ๋์ง ์ดํดํ ์ ์๋ค.
- ์ด์ ๋ชจ๋ unique_ptr์ ์ฌ์ฉ
- ์ง์ ๋ฉ๋ชจ๋ฆฌ ๊ด๋ฆฌํ๋ ๊ฒ ๋งํผ ๋น ๋ฅด๋ค.
- RAII์์น์ ์ ๋ค์ด๋ง์
- ์์ ํ ๋น์ ๊ฐ์ฑ์ ์๋ช ๊ณผ ์ฐ๊ด
- ์์ฑ์์์ new, ์๋ฉธ์์์ delete
- ์ค์ํ๊ธฐ ์ด๋ ค์
- ๋ชจ๋ ๊ณณ์ ์ฐ์!
// ๊ธฐ๋ณธ ์๋ฃํ๊น์ง๋ ์ ๋ํฌ ํฌ์ธํฐ๋ก ์ธ ์ ์๋ค.
unique_ptr<int> num1 = make_unique<int>(10);
uniquq_ptr<char> char1 = make_unique<char>('d');
cout << *num1 << endl;
cout << *char1 << endl;
// ๊ฐ์ฒด๋ก unique ptr
unique_ptr<MyVector> myVector = make_unique<MyVector>(3, 5);
cout << "X : " << myVector->GetX() << ", Y :" << myVector->GetY() << endl;
// ์์ ๊ถ ์ด์
unique_ptr<char> char2(move(char1));
//cout << "char1 :" << *char1 << endl; // Error
cout << "char2 :" << *char2 << endl;