๐Ÿ”ฅ์ฝ”๋”ฉ ๊ทœ์น™ : move semantics_์ฐธ์กฐ ์นด์šดํŒ… ์ง์ ‘ ๋งŒ๋“ค๊ธฐ.

๋ณด๋ฌผ์ฐฝ๊ณ ยท2022๋…„ 9์›” 11์ผ
0

c++Intermediate ์ฝ”๋“œ๋ˆ„๋ฆฌ

๋ชฉ๋ก ๋ณด๊ธฐ
38/62
post-thumbnail

์–•์€ ๋ณต์‚ฌ

: ํด๋ž˜์Šค ๋ฉค๋ฒ„ ๋ฐ์ดํ„ฐ๊ฐ€ ํฌ์ธํ„ฐ๊ฐ€ ์žˆ๋Š” ์ƒํƒœ์—์„œ ,
๋””ํดํŠธ ๋ณต์‚ฌ ์ƒ์„ฑ์ž๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด, ๋‘ ๊ฐ์ฒด์˜ ํฌ์ธํ„ฐ๊ฐ€
ํ•˜๋‚˜์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๊ณต์œ ํ•˜๋ฉด์„œ ๋ฐœ์ƒํ•˜๋Š” ๋ฌธ์ œ

ํ•ด๊ฒฐํ•˜๊ธฐ

: ์–•์€ ๋ณต์‚ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ

  • ๊นŠ์€ ๋ณต์‚ฌ
  • ์ฐธ์กฐ ์นด์šดํŒ… ์ด ์žˆ์Œ.

๋””ํดํŠธ ๋ณต์‚ฌ ์ƒ์„ฑ์ž์˜ ํŠน์ง•

: ๊ฐ์ฒด์˜ ๋ชจ๋“  ๋ฉค๋ฒ„๋ฅผ ๋Œ€์ž…์—ฐ์‚ฐ์ž๋กœ ์ฒ˜๋ฆฌํ•จ.
-> ์–•์€ ๋ณต์‚ฌ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒ!

problem

: char * name, int age ๋ฅผ ๋ฉค๋ฒ„๋กœ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”
Cat ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด๋ผ. name์€ strcpy๋ฅผ ์‚ฌ์šฉ

  • 1๋ฒˆ : ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๋ณต์‚ฌ ์ƒ์„ฑ์ž ๋งŒ๋“ค๊ธฐ
  • 2๋ฒˆ : ์ฐธ์กฐ ์นด์šดํŒ…์œผ๋กœ ๋งŒ๋“ค์–ด๋ผ
int main()
{
	Cat a("bom", 1);
    Cat b = a; 
    //Cat c(a);
}

- ์–•์€ ๋ณต์‚ฌ๋กœ ์ธํ•œ ๋ฌธ์ œ ๋ฐœ์ƒ ์ฝ”๋“œ
-> ์™œ๋ƒํ•˜๋ฉด ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ๋””ํดํŠธ ๋ณต์‚ฌ ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด ํฌ์ธํ„ฐ name์˜ ์ฃผ์†Œ๋ฅผ
2๊ฐœ์˜ ๊ฐ์ฒด๊ฐ€ ๊ณต์œ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž„.

  • 1๋ฒˆ. ๊นŠ์€ ๋ณต์‚ฌ๋กœ ์ฒ˜๋ฆฌํ•˜๊ธฐ
    : ๋ณต์‚ฌ ์ƒ์„ฑ์ž์—์„œ ์ƒˆ๋กœ์šด ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹นํ•˜๊ณ , ๊ฐ’์„ ๋Œ€์ž…ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

  • 2๋ฒˆ. ์ฐธ์กฐ ์นด์šดํŒ…์„ ํ™œ์šฉํ•˜์ž.
    : ๋™์ผํ•œ ํฌ์ธํ„ฐ์— ๋Œ€ํ•ด ๋ช‡๋ฒˆ์„ ์ฐธ์กฐํ•˜๊ณ  ์žˆ๋Š”์ง€๋ฅผ ์นด์šดํŒ…ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

  • c++์˜ shared_ptr์—์„œ ์‚ฌ์šฉํ•จ.

// cppTest.cpp : ์ด ํŒŒ์ผ์—๋Š” 'main' ํ•จ์ˆ˜๊ฐ€ ํฌํ•จ๋ฉ๋‹ˆ๋‹ค. ๊ฑฐ๊ธฐ์„œ ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰์ด ์‹œ์ž‘๋˜๊ณ  ์ข…๋ฃŒ๋ฉ๋‹ˆ๋‹ค.
//

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;

class Cat
{
private : 
	char* name;
	int age;
	int* refCnt;
public : 
	Cat(const char* _name, int _age)
		: age(_age)
	{
		refCnt = new int(1);
		name = new char[strlen(_name) + 1];
		strcpy(name, _name);
		cout << "refCnt ์ƒ์„ฑ " << endl;
	}

	~Cat()
	{
		
		{
			--(*refCnt);
			cout << "์นด์šดํŒ… 1 ๊ฐ์†Œ" << endl;

		}
		
		if (*refCnt == 0)
		{
			delete[] name;
			delete refCnt;
			cout << "๋ฉ”๋ชจ๋ฆฌ ํ•ด์ œ" << endl;
		}
		
	}

	Cat(const Cat& _cat)
		: age(_cat.age), name{_cat.name}
	{
		++(*_cat.refCnt);
		refCnt = _cat.refCnt;
		cout << "์นด์šดํŒ… 1 ์ฆ๊ฐ€" << endl;
		cout << "refCnt์˜ ์ฃผ์†Œ๋Š”? " << &(refCnt) << endl;
		cout << "refCnt์˜ ์ฃผ์†Œ๋Š”? " << &(_cat.refCnt) << endl;
		//name = new char[strlen(_cat.name) + 1];
		//strcpy(name, _cat.name);
	}
	void print() const
	{
		cout << name << endl;
	}
};



int main()
{
	Cat a("bom", 1);
	Cat b = a;
	
	b.print(); 
	a.print();
}

โœจ์ž„์‹œ๊ฐ์ฒด์™€ ๋ณต์‚ฌ ๋Œ€์ž…

problem

1) const char* , int ํƒ€์ž…์˜ ๋ฉค๋ฒ„๋ฅผ ๊ฐ€์ง€๊ณ , ๊ณ ์–‘์ด์˜ ์ด๋ฆ„๊ณผ ๋‚˜์ด๋ฅผ ์ž…๋ ฅํ•˜์ž.
2) ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” foo ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์ž.
3) main์—์„œ Car c(foo);
4) c.print ํ•ด์„œ ์ด๋ฆ„์„ ์ถœ๋ ฅํ•˜์ž.
5) ์ถ”๊ฐ€์ ์œผ๋กœ ์ด๋™์ƒ์„ฑ์ž ํ˜ธ์ถœ๋˜์—ˆ์„ ๋•Œ์˜ this ํฌ์ธํ„ฐ์˜ ์ฃผ์†Œ๋„ ํ™•์ธํ•ด๋ณด์ž.

  • ๋ณต์‚ฌ์™€ ์ด๋™์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค๊ณ , ๋จผ์ € ๋ณต์‚ฌ ํ…Œ์ŠคํŠธ ํ›„์— ์ด๋™ํ…Œ์ŠคํŠธ ํ•˜์ž.

    ์ด๋™ ์ƒ์„ฑ์ž์˜ ๋ชจ์–‘์€ Cat(&&) ์ด๊ณ , ๋ณต์‚ฌ ์ƒ์„ฑ์ž์ฒ˜๋Ÿผ ๋ฉค๋ฒ„๊ฐ„ ๋Œ€์ž…์„ ํ•˜๊ณ ,
    ํฌ์ธํ„ฐ๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ, ์›๋ณธ ๊ฐ์ฒด ๋ฉค๋ฒ„๋ฅผ = nullptr ์ฒ˜๋ฆฌํ•ด์„œ ์ฐธ์กฐ๊ฐ’์„ ๋‚ ๋ฆฌ์ž.

int main()
{
Cat a = foo();
a.print();
std::cout << &a << std::endl;

}


๋ณต์‚ฌ ์ƒ์„ฑ์ž์™€ ์ด๋™์ƒ์„ฑ์ž์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž.

  • ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•ด๋ณด์ž.
#include <iostream>
using namespace std;
#include <string>

#include <vector>
#include <limits.h>
#include <algorithm>
#include <map>

class Cat
{
private:
	char* name;
	int age;
public:
	Cat(const char* _name, int _age)
		: age{ age }
	{
		std::cout << "constru" << std::endl;
		name = new char[strlen(_name) + 1];
		strcpy(name, _name);
		std::cout << this << std::endl;
	}

	Cat(const Cat& _copy)
	{
		std::cout << "copy constru" << std::endl;
		name = new char[strlen(_copy.name) + 1];
		strcpy(name, _copy.name);
		std::cout << this << std::endl;

	}
	~Cat()
	{
		if (name != 0)
		{
			delete[] name;
			std::cout << "delete name! " << std::endl;
		}
		std::cout << "destructor" << std::endl;
	}

	void print()
	{
		std::cout << name << std::endl;
	}

	//Cat(Cat&& movObj)
	//{
	//	std::cout << "move Call" << std::endl;
	//	name = movObj.name;
	//	age = movObj.age;
	//	std::cout << this << std::endl;
	//	movObj.name = 0;
	//}
};

Cat foo()
{
	Cat c("naOng", 10);
	std::cout << "foo" << std::endl;
	std::cout << &c << std::endl;

	return c;
}

int main()
{
	Cat a = foo();
	a.print();
	std::cout << &a << std::endl;

}
  • ์‹คํ–‰ ๊ฒฐ๊ณผ
    : ๊ฐ์ฒด์˜ ์ฃผ์†Œ๋ฅผ ๋ณด๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, delete name์„ ๋ณด์ž.
    2๋ฒˆ ์ถœ๋ ฅ๋จ.
  • ๊ฒฐ๊ณผ !
    :

foo ํ•จ์ˆ˜ ํ˜ธ์ถœ๋กœ ์ธํ•ด c๊ฐ์ฒด๊ฐ€ ๋งŒ๋“ค์–ด์ง€๊ณ , name ํฌ์ธํ„ฐ์˜ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ํ• ๋‹น๋จ.
์ด์–ด์„œ ๋ฐ˜ํ™˜์„ ํ•˜๋ฉด์„œ a๊ฐ์ฒด์—๋‹ค๊ฐ€ ๋Œ€์ž…๋˜๋Š”๋ฐ, ์—ฌ๊ธฐ์„œ foo ์—์„œ ํ• ๋‹น๋œ c๊ฐ์ฒด๋Š” ํŒŒ๊ดด๋จ. foo ํ•จ์ˆ˜๋Š” ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ž„์‹œ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๋ฉด์„œ ๋ณต์‚ฌ ์ƒ์„ฑ์ž์˜ ์ธ์ž์— ์ „๋‹ฌํ•จ.
์—ฌ๊ธฐ์„œ ์ค‘์š”ํ•œ ๋ถ€๋ถ„์ด ๋ณต์‚ฌ ์ƒ์„ฑ์ž๋ฅผ ์ง„์ž…ํ•˜๋ฉด์„œ 2๋ฒˆ์งธ name ํฌ์ธํ„ฐ์˜ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ํ• ๋‹น๋จ.
๋ณต์‚ฌ ์ƒ์„ฑ์ž๋กœ ์ธํ•ด์„œ name ์ž์›์ด 2๋ฒˆ์ด๋‚˜ ํ• ๋‹น๋˜๋Š”๋ฐ, foo ํ•จ์ˆ˜์—์„œ ๋ฐ˜ํ™˜ํ•˜๊ณ  ๋ฐ”๋กœ ์‚ฌ๋ผ์ง€๋Š” ๊ฐ์ฒด c์˜ name ์ž์›์„ ์ž˜ ์‚ฌ์šฉํ•˜๋ฉด ์ข‹์ง€ ์•Š์„๊นŒ? ๋ž€ ์ƒ๊ฐ์„ ํ•ด์•ผ ํ•œ๋‹ค!

  • ์ด๋™์ƒ์„ฑ์ž๋ž€?
    ์ด๋Ÿฌํ•œ ๋ถ€๋ถ„์„ ๊ฐœ์„ ํ•˜๊ธฐ ์œ„ํ•ด c++11์—์„œ ๋„์ž…๋œ ๊ฒƒ์ด ์ด๋™์ƒ์„ฑ์ž์ด๋‹ค.
    ์ด๋™ ์ƒ์„ฑ์ž๋Š” ์ž์›์„ ๋ณต์‚ฌํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ. ์ด๋™ํ•˜๋Š” ๊ฒƒ์ž„.
    ์–•์€ ๋ณต์‚ฌ์ฒ˜๋Ÿผ ํฌ์ธํ„ฐ ์ฃผ์†Œ๋งŒ ๋„˜๊ฒจ์ฃผ๊ณ , ์›๋ณธ ๋ฐ์ดํ„ฐ์—์„œ๋Š” ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํฌ๊ธฐํ•˜๋ฉด ๋จ.

  • ๊ณ ์ฐฐ:
    ์ฐจ๋ผ๋ฆฌ foo ํ•จ์ˆ˜์—์„œ ๊ฐ์ฒด c๊ฐ€ ํ• ๋‹น๋œ name ์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๊ฐ์ฒด a์˜ name์—๋‹ค๊ฐ€ ์ž์› ์ด๋™ํ•˜๋ฉด
    ๋ณต์‚ฌ ์ƒ์„ฑ์ž์—์„œ 2๋ฒˆ์งธ๋กœ name ํฌ์ธํ„ฐ๊ฐ€ ํ• ๋‹น๋˜๋Š” ์ž์›์„ ์•„๋‚„ ์ˆ˜ ์žˆ์Œ.
    ์—ฌ๊ธฐ์„œ ์•Œ์•„์•ผ ๋˜๋Š” ๋ถ€๋ถ„์ด ์ด๋™์ƒ์„ฑ์ž๋‚˜ ๋ณต์‚ฌ ์ƒ์„ฑ์ž๋‚˜ ์–ด์จ‹๋“  ํ˜ธ์ถœํ•˜๋ฉด
    ๊ฐ์ฒด๋Š” ์ƒ์„ฑ๋œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. name ์ž์›์ด 2๋ฒˆ ํ• ๋‹น๋˜๋Š” ๊ฒƒ์„ 1๋ฒˆ๋งŒ ํ• ๋‹นํ•˜์ž๋Š” ๊ฒƒ์ด ์ด๋™์ƒ์„ฑ์ž์˜ ๋ชฉ์ ์ด๋‹ค.

  • ์ด๋™ ์ƒ์„ฑ์ž ์žˆ์„ ๋•Œ์˜ ์ฝ”๋“œ์™€ ๊ฒฐ๊ณผ
#include <iostream>
#include <vector>
//using namespace std;
//#include <stdio.h>
#include <winsock2.h>
#include <thread>
#pragma  comment(lib, "ws2_32")

#define _WINSOCK_DEPRECATED_NO_WARNINGS

#include <Windows.h>

#include <memory>
using namespace std;

#include <vector>
#include <algorithm>

class Cat
{
private : 
	char* name;
	int age;
public: 
	Cat(const char* _name, int _age)
		: age{age}
	{
		std::cout << "constru" << std::endl;
		name = new char[strlen(_name) + 1];
		strcpy(name, _name);
		std::cout << this << std::endl;
	}

	Cat(const Cat& _copy)
	{
		std::cout << "copy constru" << std::endl;
		name = new char[strlen(_copy.name) + 1];
		strcpy(name, _copy.name);
		std::cout << this << std::endl;

	}
	~Cat()
	{
		if (name != 0)
		{
			delete[] name;
			std::cout << "delete name! " << std::endl;
		}
		std::cout << "destructor" << std::endl;
	}

	void print()
	{
		std::cout << name << std::endl;
	}

	Cat(Cat&& movObj)
	{
		std::cout << "move Call" << std::endl;
		name = movObj.name;
		age = movObj.age;
		std::cout << this << std::endl;
		movObj.name = 0;
	}
};

Cat foo()
{
	Cat c("naOng", 10);
	std::cout << "foo" << std::endl;
	std::cout << &c << std::endl;
	
	return c;
}

int main()
{
	Cat a = foo();
	a.print();
	std::cout << &a << std::endl;

}


์ด๋™์ƒ์„ฑ์ž ์กด์žฌ์‹œ ์ •๋ฆฌ

// ์œ„์˜ ํ”„๋กœ์ ํŠธ๋Š” ์ฝ”๋“œ ์ตœ์ ํ™”๊ฐ€ ์•ˆ๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ์ด๋‹ค. 240803

  • ์ผ๋‹จ ์™œ ์ด๋™์ƒ์„ฑ์ž๊ฐ€ ํ˜ธ์ถœ๋˜์—ˆ๋ƒ๋ฉด? ์ž„์‹œ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์žˆ๊ธฐ
    ๋•Œ๋ฌธ์ด๋ผ๋Š” ๊ฒƒ์„ ํŒŒ์•…ํ•ด์•ผ ํ•œ๋‹ค!
  • ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๊ณ , ๊ฐ์ฒด์˜ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์ด๋™ํ•˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๊ณ 
    ์ž์›์„ ์ด๋™์‹œํ‚ค๊ฒŒ ํ•˜๋Š” ๊ฒƒ์ž„.
    ๊ทธ๋ฆฌ๊ณ  delete name์€ ํ•œ๋ฒˆ๋งŒ ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•˜๋ฉด์„œ ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น 1๋ฒˆ๋งŒ ํ•จ1
  • ์ด๋™ ์ƒ์„ฑ์ž๋ƒ ๋ณต์‚ฌ ์ƒ์„ฑ์ž๋ƒ?
    : ๋ฐ”๋กœ ํ•ด์ œ ๋˜๋Š” ์ž„์‹œ๊ฐ์ฒด์˜ ๊ฒฝ์šฐ, ๋ฉค๋ฒ„ ๋‚ด ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น ์ด์Šˆ๊ฐ€ ์žˆ์œผ๋‹ˆ๊นŒ,
    ์ด๋™ ์ƒ์ •์ž๊ฐ€ ๋ณต์‚ฌ๋ณด๋‹ค๋Š” ์ข‹์Œ.

๊ฒฐ๋ก 
-> foo์—์„œ ๋ฐ˜ํ™˜๋˜๋Š” ๊ฐ์ฒด c๊ฐ€ ๊ฐ’ํƒ€์ž…์œผ๋กœ ๋ฐ˜ํ™˜๋จ.
์ปดํŒŒ์ผ๋Ÿฌ๋Š” ์ง€์—ญ ๊ฐ์ฒด์ธ c๊ฐ€ fooํ•จ์ˆ˜์—์„œ ์ œ๊ฑฐ๋˜๋ฏ€๋กœ, ๋ณต์‚ฌ์ƒ์„ฑ์ž๋ฅผ ์•”์‹œ์ ์œผ๋กœ ํ˜ธ์ถœํ•จ.
: foo() ํ•จ์ˆ˜์˜ ๊ฒฝ์šฐ, "hello" ๊ฐ€ ํ˜ธ์ถœ๋˜๊ธฐ๋„ ์ „์— ์ž„์‹œ๊ฐ์ฒด๊ฐ€
์†Œ๋ฉธ๋จ.
๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์ƒ์„ฑ,ํ•ด์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค๋Š” ๊ฒƒ์ž„.
์ฐจ๋ผ๋ฆฌ foo์—์„œ ๋ฐ˜ํ™˜๋˜๋Š” ์ž„์‹œ๊ฐ์ฒด์˜ ์ž์›์„ ๊ทธ๋Œ€๋กœ ๊ฐ์ฒด cc๊ฐ€ ๋ฐ›๋Š”๋‹ค๋ฉด,
๋ฉ”๋ชจ๋ฆฌ ๊ด€์ ์—์„œ ์„ฑ๋Šฅ์„ ๊ฐ€์ง€๊ณ  ์˜ฌ ์ˆ˜ ์žˆ์Œ.

240803 ์ถ”๊ฐ€ ์ตœ์ ํ™”๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ.

: ์œ„์˜ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด, move ์ฒ˜๋ฆฌ๋˜์ง€ ์•Š๊ณ , ์ตœ์ ํ™”๋˜์–ด์„œ ๋ฐ”๋กœ ์ƒ์„ฑ์ž ํ•˜๋‚˜๋งŒ ํ˜ธ์ถœ๋œ๋‹ค.

์ด๋™ ์ƒ์„ฑ์ž

: ์ด๋™์ƒ์ •์ž๋Š” ๊ฐ์ฒด ๋‚ด์˜ ๋ฉค๋ฒ„ ์ž์›์„ ์ด๋™์‹œํ‚ค๋Š” ๊ฒƒ์ด์ง€, ๊ฐ์ฒด์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ
ํ†ต์งธ๋กœ ์˜ฎ๊ธฐ๋Š” ๊ฒƒ์€ ์•„๋‹˜/

์ด๋™์ƒ์„ฑ์ž๋ฅผ ์–ด๋–ป๊ฒŒ ๋งŒ๋“ค๊นŒ?

(์ด๋™์ƒ์„ฑ์ž ์ž‘์„ฑํ•˜์ง€ ์•Š์•˜์„ ๋•Œ) ์œ„์—์„œ ์ž„์‹œ๊ฐ์ฒด๊ฐ€ call ๋˜๋ฉด์„œ ๋ณต์‚ฌ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์—ˆ์Œ.
์ž„์‹œ๊ฐ์ฒด๋Š” Rvalue์ด๋ฏ€๋กœ, const& Obj ํƒ€์ž…๋ณด๋‹ค ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’์€ Obj&& ์ธ์ž๋กœ
๋˜์–ด ์žˆ๋Š” ์ด๋™์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜๊ฒŒ ํ•˜์ž.
- ์ถ”๊ฐ€์ ์œผ๋กœ ์ž์›์„ ๊ทธ๋Œ€๋กœ ์ด๋™ํ•˜๊ณ , ์›๋ณธ ๊ฐ์ฒด๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ์ž‘์—…์„ ํ•ด์•ผํ•จ.
- 1) ๋ฉค๋ฒ„ ์ž์›์„ ๊ทธ๋ƒฅ ๋ณต์‚ฌ ๋Œ€์ž…ํ•œ ํ›„,
- 2) ์ธ์ž๋กœ ๋ฐ›์€ ๊ฒƒ์ค‘์—์„œ ํฌ์ธํ„ฐ๊ฐ€ ์žˆ๋‹ค๋ฉด, ํฌ์ธํ„ฐ๋ฅผ 0์œผ๋กœ ์„ค์ •

0์œผ๋กœ ์„ค์ •ํ•˜๊ฒŒ ๋˜๋ฉด,

  • delete ์‹œ์— ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ 0๋“ฑ์˜ ์—†๋Š” ๊ฒฝ์šฐ ๋ฌธ์ œ ๋ฐœ์ƒํ•˜์ง€ ์•Š๊ณ , ๊ทธ๋ƒฅ ์Šคํ‚ตํ•œ๋‹ค๊ณ  ํ•œ๋‹ค.

  • ์˜ˆ์‹œ์ฝ”๋“œ์˜ ์„ค๋ช… ๋Œ€๋กœ ํ•˜๊ธฐ ์œ„ํ•œ ๊ฐœ๋…
    : c++์—์„œ ์ฐธ์กฐ๋˜๋Š” ํฌ์ธํ„ฐ๊ฐ€ 0์ด๋ผ๋ฉด, ๋ฉค๋ฒ„ ๋ฐ์ดํ„ฐ์˜ ํ•ด์ œ๋Š”
    ๋ฐœ์ƒํ•˜์ง€ ์•Š์Œ. ๋”ฑํžˆ ์กฐ๊ฑด ์ฒ˜๋ฆฌ์•ˆํ•ด๋„ ๋จ.

  • ๊ทธ๋ฆผ

    • ๋™์ผํ•œ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ฐธ์กฐํ•˜๊ณ  ์žˆ์Œ.
  • ์ด ์ƒํ™ฉ์—์„œ ์ž„์‹œ๊ฐ์ฒด์ชฝ์˜ ์ฐธ์กฐ๋ฅผ 0์œผ๋กœ ํ•˜์ž.

์ด๋™ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•ด์„œ ์ž์›์„ ์•„๋ผ๋Š” ๋ฐฉ๋ฒ•.

  1. ์ด๋ฏธ ๋งŒ๋“ค์–ด์ง„ name์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ƒˆ๋กœ์šด ๊ฐ์ฒด::name์ด ์ฐธ์กฐํ•˜๊ฒŒ ํ•œํ›„
  2. ์ธ์ž๋กœ ๋“ค์–ด์˜ค๋Š” ๊ฐ์ฒด์˜ name์ด ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฐ’์„ 0์œผ๋กœ ์„ค์ •ํ•˜์ž.

์ด๋™ ์ƒ์„ฑ์ž์˜ ๋ชจ์–‘

๋ณต์‚ฌ ์ƒ์„ฑ์ž์˜ ์ธ์ž์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•˜๊ธฐ์—๋Š” lvalue๋กœ ๋ณด๋ƒˆ์„ ๋•Œ๋Š”
์†Œ๋ฉธ๋˜์ง€ ์•Š๊ณ  , ๊ณ„์†ํ•ด์„œ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ!

->์ฆ‰, rvalue์— ํ•œํ•ด์„œ๋งŒ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผ ํ•˜๋ฏ€๋กœ
(Point &&p) ํ˜•ํƒœ๋กœ ์ธ์ž๋ฅผ ์„ค์ •ํ•ด์•ผ ํ•จ.
-> ๋”ฐ๋ผ์„œ ๋ณต์‚ฌ ์ƒ์„ฑ์ž์™€ ์ด๋™ ์ƒ์„ฑ์ž 2๊ฐœ๋ฅผ ๋งŒ๋“ค์–ด์•ผ ํ•จ.
--> ์ด๋™์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค์ง€ ์•Š์œผ๋ฉด, ์ž„์‹œ๊ฐ์ฒด๋ฅผ ๋ณต์‚ฌ์ƒ์„ฑ์ž๊ฐ€ ์‚ฌ์šฉํ•˜๊ฒŒ ๋œ๋‹ค.

์ด๋™ ์ƒ์„ฑ์ž์˜ ์ •์˜

: rvalue๋ฅผ ์ธ์ž๋กœ ๋ฐ›์•„์„œ ์›๋ณธ ๊ฐ์ฒด์˜
์†Œ์œ ๊ถŒ, ์ž์›์„ ์ด์ „ํ•˜๊ฒŒ ํ•˜๋Š” ์ƒ์„ฑ์ž๋ฅผ ๋งํ•จ.
ํฌ์ธํ„ฐ์˜ ๊ฒฝ์šฐ๋Š” ์ฃผ์†Œ๋ฅผ ๋„˜๊ธฐ๊ณ , ์›๋ณธ ๊ฐ์ฒด๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ํฌ์ธํ„ฐ๋ฅผ nullptr ์ฒ˜๋ฆฌํ•ด์•ผ ํ•จ.

์ด๋™ ์ƒ์„ฑ์ž๋Š” ์–ธ์ œ ์‚ฌ์šฉํ• ๊นŒ?

: ๊ฐ์ฒด์˜ ์†Œ์œ ๊ถŒ์„ ์ด์ „ํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•จ.

์ค‘์š”

์ด๋™ ์ˆ˜ํ–‰ ํ›„, ๋ฐ˜๋“œ์‹œ ํ•ด์•ผํ•  ๋™์ž‘.
: ์›๋ณธ์˜ ํฌ์ธํ„ฐ๋Š” ๋ฐ˜๋“œ์‹œ 0๊ฐ’์œผ๋กœ ๋งŒ๋“ค์–ด์•ผ ํ•จ!
-> 0์œผ๋กœ ํ•˜๋ฉด ์†Œ๋ฉธ์ž์—์„œ delete ํ•˜๋Š” ์ฝ”๋“œ๊ฐ€ ์žˆ๋”๋ผ๋„ ์•„๋ฌด์ผ ์—†์ด ์ง€๋‚˜๊ฐ„๋‹ค.

problem

: ๊ทธ๋ฆผ 1๋ฒˆ์„ ํƒ€์ดํ•‘ ํ•œํ›„, ์ด๋™์ฒ˜๋ฆฌํ•ด์„œ ์ž์› name ์ด
๋‹ค์‹œ ํ•œ๋ฒˆ ๋™์ ํ• ๋‹น๋˜๋Š” ๊ฒƒ์„ ์ฐจ๋‹จํ•˜์ž.

  • ๊ทธ๋ฆผ 1๋ฒˆ : ์ด๋™ ์ƒ์„ฑ์ž๊ฐ€ ์—†์„ ๊ฒฝ์šฐ
    : ์ž„์‹œ ๊ฐ์ฒด ์ƒ์„ฑ, ํ•ด์ œ๊ฐ€ ๋ฐœ์ƒํ•จ.

์ค‘์š” move ์ดํ›„ ์›๋ณธ ํฌ์ธํ„ฐ๋Š” 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•ด์•ผ ํ•จ!

**- ๊ทธ๋ฆผ 2๋ฒˆ : ์ด๋™ ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค ๊ฒฝ์šฐ.

๋ฐ˜๋“œ์‹œ ์›๋ณธ ํฌ์ธํ„ฐ์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ 0์œผ๋กœ ๋งŒ๋“ค์–ด์•ผ ํ•จ!

  • ๊ฒฐ๋ก  : ์ž์›์„ ์ด๋™ํ–ˆ๊ณ , ๋‚ด๊ฐ€ ์†Œ๋ฉธ์ž ์ถœ๋ ฅํ™•์ธ์„ ์œ„ํ•ด !=0 ์กฐ๊ฑด์ฒ˜๋ฆฌ ํ–ˆ์ง€๋งŒ,
    ์กฐ๊ฑด ์ฒ˜๋ฆฌ๋ฅผ ํ•˜์ง€ ์•Š๋”๋ผ๋„, ํ•ด์ œ๊ฐ€ ๋˜์ง€ ์•Š๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์—ˆ์Œ.
    -> ์™€์šฐ! ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์ผ์„ ์•Œ์•„์„œ ์ž˜ํ•˜๋„ค???!! ใ…‹ใ…‹ใ…‹ใ…‹
    -> ์ด์œ ๊ฐ€ ์žˆ๋‹ค. c++11 ์ฐฝ์‹œ์ž๋‹˜์˜ ์ฑ…์„ ๋ณด๋ฉด, delete ๋Š” ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์ด๋ฏธ ์—†๋Š” ๊ฐ์ฒด์— ํ•œํ•ด์„œ๋Š” ๋™์ž‘์„ ํ•˜์ง€ ์•Š๋Š”๋‹ค๊ณ  ํ•œ๋‹ค. 240803

์ •๋ฆฌ

  • ๋ณต์‚ฌ ์ƒ์„ฑ์ž๋Š” cont &point ํ˜•ํƒœ์ด๊ธฐ ๋•Œ๋ฌธ์—
    lvalue, rvalue 2๊ฐœ ๋ชจ๋‘ ๋ฐ›์„ ์ˆ˜ ์žˆ์Œ.

  • ์ด๋™์ƒ์„ฑ์ž๋Š” point&& ์ด๊ธฐ ๋•Œ๋ฌธ์— rvalue๋งŒ ๋ฐ›์„ ์ˆ˜ ์žˆ์Œ.

์šฐ์„ ์ˆœ์œ„

: rValue๋ฅผ ์ธ์ž๋กœ ๋ณด๋‚ผ๋•Œ ํ˜ธ์ถœ๋˜๋Š” ์šฐ์„ ์ˆœ์œ„.
์ด๋™ ์ƒ์„ฑ์ž > ๋ณต์‚ฌ ์ƒ์„ฑ์ž.

  • ๋งŒ์•ฝ ์ž„์‹œ๊ฐ์ฒด๋กœ ์ดˆ๊ธฐํ™”ํ•  ๊ฒฝ์šฐ, ์ด๋™์ƒ์„ฑ์ž๋‚˜ ๋ณต์‚ฌ ์ƒ์„ฑ์ž๊ฐ€ ํ˜ธ์ถœ๋จ.
    1) ์ด๋™ ์ƒ์„ฑ์ž๊ฐ€ ์žˆ๋‹ค๋ฉด, ์†Œ์œ ๊ถŒ ์ด์ „ ๋จ. , 1๋ฒˆ๋ณด๋‹ค๋Š” ํšจ์œจ์ ์ž„.
    2) ์ด๋™์ƒ์„ฑ์ž๊ฐ€ ์—†๋‹ค๋ฉด, ๋ณต์‚ฌ ๋ฐœ์ƒ
    3) ์ด๋™์ƒ์„ฑ์ž๊ฐ€ ์—†๋‹ค๊ณ  ํ•ด๋„, ์—๋Ÿฌ๋Š” ์•„๋‹˜

copy ellipse

// move ์ƒ์„ฑ์ž๋ฅผ ์ฃผ์„ ์ฒ˜๋ฆฌํ•˜์ž.

  • ๋ณต์‚ฌ ์ตœ์ ํ™”
    : ์ž„์‹œ๊ฐ์ฒด๋ฅผ ๋ณต์‚ฌํ•  ๊ฒฝ์šฐ, ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ๋ณต์‚ฌ๋ฅผ ํ˜ธ์ถœํ•˜์ง€ ์•Š๊ณ , ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•จ.
int main()
{
	point p;
    point p2 = p;
    point p3 = point(); 
}
  • ๋น„์ฃผ์–ผ์˜ ๊ฒฝ์šฐ, ๋ณต์‚ฌ์ตœ์ ํ™”๊ฐ€ ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ํ•จ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์„ ๋Œ€์ž…ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ rvalue๋ฅผ ๋ณต์‚ฌ ์ƒ์„ฑ์ž์—์„œ ํ˜ธ์ถœ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Œ.
int main()
{
	point p = foo();
}

๊ฒฐ๋ก 

  • ํด๋ž˜์Šค๋ฅผ ์„ค๊ณ„ํ•  ๋•Œ, ์ด๋™์ƒ์„ฑ์ž, ๋ณต์‚ฌ ์ƒ์„ฑ์ž 2๊ฐœ๋ฅผ ๋ชจ๋‘ ๋งŒ๋“ค์ž!
profile
๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅ

0๊ฐœ์˜ ๋Œ“๊ธ€