Posted at

Effective C++ Third version, sample code compile list(13)Use objects to manage resources

More than 1 year has passed since last update.

Effective C++: 55 Specific Ways to Improve Your Programs and DesignsThird version by Scott Mayers

https://www.informit.com/store/effective-c-plus-plus-55-specific-ways-to-improve-your-9780321334879

https://www.amazon.com/dp/0321334876

Sample code from

Yuzhen11/Effective_CPP

https://github.com/Yuzhen11/Effective_CPP


算譜(source code)


ec13.cpp

///Effective C++:  55 Specific Ways to Improve Your Programs and DesignsThird version by Scott Mayers

///https://www.informit.com/store/effective-c-plus-plus-55-specific-ways-to-improve-your-9780321334879
#include <iostream>
#include <cstdlib>
#include <vector>
using namespace std;

const char * msg ="Item 13: Use objects to manage resources";

/// From here "Effective C++

///namespace A{

class Investment {
};
Investment * createInvestment() {};

void f() {
Investment* pInv = createInvestment(); // call factory function
/// ... // something bad may happen
delete pInv; // release object
}
//Put that resource inside an object whose destructor will automatically release the resource when control leaves f.
///}

namespace B {
void f() {
std::auto_ptr<Investment> pInv(createInvestment()); // call factory function
}
//auto_ptrs have an unusual characteristic: copying them sets them null.
}

void g1() {
std::auto_ptr<Investment> pInv1(createInvestment);
std::auto_ptr<Investment> pInv2(pInv1); // pInv2 now points to the object; pInv1 is now null

pInv1 = pInv2; // now pInv1 points to the object, and pInv2 is null
//shared_ptr behaves well.
}

void g2() {
std::tr1::shared_ptr<Investment> pInv1(createInvestment);
std::tr1::shared_ptr<Investment> pInv2(pInv1); // both pInv1 and pInv2 now point to the object

pInv1 = pInv2; // ditto
}

//Two critical aspects of using objects to managing resources:

//Resources are acquired and immediately turned over to resource-managing objects.
//Resource-managing objects use their destructors to ensure that resources are released.

//Things to Remember

//To prevent resource leaks, use RAII objects that acquire resource in their constructors and release them in their destructors.
//Two commonly useful RAII classes are tr1::shared_ptr and auto_ptr. tr1::shared_ptr is usually the better choice, because its behavior when copied is intuitive. copying an auto_ptr sets it to null.

/// to here from "Effective C++"

int main() {
cout << msg << endl;
return EXIT_SUCCESS;
}



編纂・実行結果(compile and go)


cpa.sh

$ ../c1.sh ec13

$ clang++ ec13.cpp -std=c++2a -Wall
ec13.cpp:16:33: warning: control reaches end of non-void function [-Wreturn-type]
Investment * createInvestment(){};
^
ec13.cpp:28:10: error: no member named 'auto_ptr' in namespace 'std'
std::auto_ptr<Investment> pInv(createInvestment()); // call factory function
~~~~~^
ec13.cpp:28:19: error: 'Investment' does not refer to a value
std::auto_ptr<Investment> pInv(createInvestment()); // call factory function
^
ec13.cpp:14:7: note: declared here
class Investment {
^
ec13.cpp:28:31: error: use of undeclared identifier 'pInv'
std::auto_ptr<Investment> pInv(createInvestment()); // call factory function
^
ec13.cpp:34:6: error: no member named 'auto_ptr' in namespace 'std'
std::auto_ptr<Investment> pInv1(createInvestment);
~~~~~^
ec13.cpp:34:15: error: 'Investment' does not refer to a value
std::auto_ptr<Investment> pInv1(createInvestment);
^
ec13.cpp:14:7: note: declared here
class Investment {
^
ec13.cpp:34:27: error: use of undeclared identifier 'pInv1'
std::auto_ptr<Investment> pInv1(createInvestment);
^
ec13.cpp:35:6: error: no member named 'auto_ptr' in namespace 'std'
std::auto_ptr<Investment> pInv2(pInv1); // pInv2 now points to the object; pInv1 is now null
~~~~~^
ec13.cpp:35:15: error: 'Investment' does not refer to a value
std::auto_ptr<Investment> pInv2(pInv1); // pInv2 now points to the object; pInv1 is now null
^
ec13.cpp:14:7: note: declared here
class Investment {
^
ec13.cpp:35:33: error: use of undeclared identifier 'pInv1'
std::auto_ptr<Investment> pInv2(pInv1); // pInv2 now points to the object; pInv1 is now null
^
ec13.cpp:37:1: error: use of undeclared identifier 'pInv1'
pInv1 = pInv2; // now pInv1 points to the object, and pInv2 is null
^
ec13.cpp:37:9: error: use of undeclared identifier 'pInv2'
pInv1 = pInv2; // now pInv1 points to the object, and pInv2 is null
^
ec13.cpp:42:6: error: no member named 'tr1' in namespace 'std'
std::tr1::shared_ptr<Investment> pInv1(createInvestment);
~~~~~^
ec13.cpp:43:6: error: no member named 'tr1' in namespace 'std'
std::tr1::shared_ptr<Investment> pInv2(pInv1); // both pInv1 and pInv2 now point to the object
~~~~~^
1 warning and 13 errors generated.



課題(agenda)

1)役に立つまたは意味のあるコンパイルエラーを取る方法

2)役に立つまたは意味のある関数、クラスの呼び出し方

3)役に立つまたは意味のある出力


参考資料(reference)

docker gnu(gcc/g++) and llvm(clang/clang++)

https://qiita.com/drafts/059874ea39c4de64c0f7

[C][C++]の国際規格案の例題をコンパイルするときの課題7つ。

https://qiita.com/kaizen_nagoya/items/5f4b155030259497c4de

C++N4606 Working Draft 2016, ISO/IEC 14882, C++ standardのコード断片をコンパイルするためにしていること

https://qiita.com/kaizen_nagoya/items/a8d7ee2f2e29e76c19c1

コンパイル用shell script C版(clangとgcc)とC++版(clang++とg++)

https://qiita.com/kaizen_nagoya/items/74220c0577a512c2d7da

Clang/Clang++(LLVM) gcc/g++(GNU) コンパイラ警告等比較

https://qiita.com/kaizen_nagoya/items/9a82b958cc3aeef0403f

C++2003とC++2017でコンパイルエラーになるならない事例集

https://qiita.com/kaizen_nagoya/items/a13ea3823441c430edff

Qiitaに投稿するCのStyle例(暫定)

https://qiita.com/kaizen_nagoya/items/946df1528a6a1ef2bc0d

cpprefjpのdecltypeをコンパイル試験

https://qiita.com/kaizen_nagoya/items/090909af702f0d5d8a67

MISRA C++ 5-0-16

https://qiita.com/kaizen_nagoya/items/7df2d4e05db724752a74

C++ Templates Part1 BASICS Chapter 3. Class Templates 3.2 Use of Class Template Stack stack1test.cpp

https://qiita.com/kaizen_nagoya/items/cd5fc49106fad5a4e9ed

ISO/IEC TS 17961:2013 C Secure Coding Rules(1) All list(to be confirmed)

https://qiita.com/kaizen_nagoya/items/54e056195c4f11b850a1

C言語(C++)に対する誤解、曲解、無理解、爽快。

https://qiita.com/kaizen_nagoya/items/3f3992c9722c1cee2e3a

C Puzzle Bookの有り難み5つ、C言語規格及びCコンパイラの特性を認識

https://qiita.com/kaizen_nagoya/items/d89a48c1536a02ecdec9

'wchar.h' file not found で困った clang++ macOS

https://qiita.com/kaizen_nagoya/items/de15cd46d657517fac11

Open POSIX Test Suiteの使い方を調べはじめました

https://qiita.com/kaizen_nagoya/items/644d5e407f5faf96e6dc

MISRA-C 2012 Referenceに掲載している文献の入手可能性を確認

https://qiita.com/kaizen_nagoya/items/96dc8b125e462d5575bb

どうやって MISRA Example Suiteをコンパイルするか

https://qiita.com/kaizen_nagoya/items/fbdbff5ff696e2ca7f00

MISRA C まとめ #include

https://qiita.com/kaizen_nagoya/items/f1a79a7cbd281607c7c9

「C++完全理解ガイド」の同意できること上位10

https://qiita.com/kaizen_nagoya/items/aa5744e0c4a8618c7671

C++参考資料一覧

https://researchmap.jp/joub9b3my-1797580/#_1797580


文書履歴(document history)

ver. 0.10 初稿 20180622