0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

std:shared_ptrの代入

Posted at

##shared_ptrにresetや代入して動かしてみた(ctor/dtor)

SharedPtrTestという何の変哲もないクラスを作って実験

その1

{
    std::shared_ptr<SharedPtrTest> sharedPtr = nullptr;

	auto temp_SharedPtr1(new SharedPtrTest(L"1"));
    sharedPtr.reset(temp_SharedPtr1);

	auto temp_SharedPtr2(new SharedPtrTest(L"2"));
	sharedPtr.reset(temp_SharedPtr2);

	auto temp_SharedPtr3(new SharedPtrTest(L"3"));
	sharedPtr.reset(temp_SharedPtr3);

	auto temp_SharedPtr4(new SharedPtrTest(L"4"));
	sharedPtr.reset(temp_SharedPtr4);
}

その2

{
    std::shared_ptr<SharedPtrTest> sharedPtr = nullptr;

	SharedPtrTest* temp_SharedPtr1 = new SharedPtrTest(L"1");
	sharedPtr.reset(temp_SharedPtr1);

	SharedPtrTest* temp_SharedPtr2 = new SharedPtrTest(L"2");
	sharedPtr.reset(temp_SharedPtr2);

	SharedPtrTest* temp_SharedPtr3 = new SharedPtrTest(L"3");
	sharedPtr.reset(temp_SharedPtr3);

	SharedPtrTest* temp_SharedPtr4 = new SharedPtrTest(L"4");
	sharedPtr.reset(temp_SharedPtr4);
}

その3

{
    std::shared_ptr<SharedPtrTest> sharedPtr = nullptr;

    std::shared_ptr<SharedPtrTest> temp_SharedPtr1(new SharedPtrTest(L"1"));
	sharedPtr = temp_SharedPtr1;

	std::shared_ptr<SharedPtrTest> temp_SharedPtr2(new SharedPtrTest(L"2"));
	sharedPtr = temp_SharedPtr2;

	std::shared_ptr<SharedPtrTest> temp_SharedPtr3(new SharedPtrTest(L"3"));
	sharedPtr = temp_SharedPtr3;

	std::shared_ptr<SharedPtrTest> temp_SharedPtr4(new SharedPtrTest(L"4"));
	sharedPtr = temp_SharedPtr4;
}

###その1の実行結果

id[1] : Ctor 
id[2] : Ctor 
id[1] : Dtor 
id[3] : Ctor 
id[2] : Dtor 
id[4] : Ctor 
id[3] : Dtor 
id[4] : Dtor 

###その2の実行結果

id[1] : Ctor 
id[2] : Ctor 
id[1] : Dtor 
id[3] : Ctor 
id[2] : Dtor 
id[4] : Ctor 
id[3] : Dtor 
id[4] : Dtor 

###その3の実行結果

id[1] : Ctor 
id[2] : Ctor 
id[3] : Ctor 
id[4] : Ctor 
id[3] : Dtor 
id[2] : Dtor 
id[1] : Dtor 
id[4] : Dtor 
0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?