From c589f76d1fc1b915eeda93870d8b9a6009f4d44c Mon Sep 17 00:00:00 2001 From: pezy-pc Date: Tue, 13 Jan 2015 21:56:58 +0800 Subject: [PATCH] added 13.1.3 --- README.md | 3 +- ch07/ex7_41.h | 2 +- ch13/13.6.7.8.cpp | 74 ----------------------------------------------- ch13/README.md | 32 ++++++++++++++++++++ ch13/ex13.7.8.cpp | 74 ----------------------------------------------- ch13/ex13_08.h | 6 ++-- ch13/ex13_11.h | 34 ++++++++++++++++++++++ ch13/ex13_13.cpp | 47 ++++++++++++++++++++++++++++++ 8 files changed, 119 insertions(+), 153 deletions(-) delete mode 100644 ch13/13.6.7.8.cpp delete mode 100644 ch13/ex13.7.8.cpp create mode 100644 ch13/ex13_11.h create mode 100644 ch13/ex13_13.cpp diff --git a/README.md b/README.md index 4deabe06..87a62d96 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -#C++ Primer (第5版) 习题答案 [![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/pezy/cpp-primer/trend.png)](https://bitdeli.com/free "Bitdeli Badge") +#C++ Primer (第5版) 习题答案 ##C++ Primer (5th Edition) exercise answers. [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/pezy/Cpp-Primer?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) @@ -47,4 +47,3 @@ 1. [PO在StackOverflow上的习题](http://book.douban.com/review/6500246/) 2. [C++ Primer issues(豆瓣讨论组)](http://www.douban.com/group/532124/) -3. [《C++ Primer 英文版(第 5 版)》的笔记 by](http://book.douban.com/people/pezy/annotation/24089577/) [@pezy](https://github.com/pezy) diff --git a/ch07/ex7_41.h b/ch07/ex7_41.h index dbce559c..2c584691 100644 --- a/ch07/ex7_41.h +++ b/ch07/ex7_41.h @@ -32,7 +32,7 @@ class Sales_data { Sales_data(std::istream &is); - std::string isbn() const { return bookNo; }; + std::string isbn() const { return bookNo; } Sales_data& combine(const Sales_data&); private: diff --git a/ch13/13.6.7.8.cpp b/ch13/13.6.7.8.cpp deleted file mode 100644 index 436f131f..00000000 --- a/ch13/13.6.7.8.cpp +++ /dev/null @@ -1,74 +0,0 @@ -/*************************************************************************** - * @file main.cpp - * @author Alan.W - * @date 30 DEC 2013 - * @remark - ***************************************************************************/ -//! -//! Exercise 13.6: -//! What is a copy-assignment operator? When is this operator used? What does -//! the synthesized copy-assignment operator do? When is it synthesized? -// A copy-assignment operator is an overloaded operator which controls how -// objects of its class are assigned. -// The compiler generates a synthesized copy-assignment operator if no user- -// defined version is provided. -// Analogously to the copy constructor, for some classes the synthesized copy- -// assignment operator disallows assignment (§ 13.1.6, p. 508). Otherwise, it -// assigns each nonstatic member of the right-hand object to the corresponding -// member of the left-hand object using the copy-assignment operator for the -// type of that member. Array members are assigned by assigning each element -// of the array. The synthesized copy-assignment operator returns a reference -// to its left-hand object. -//! -//! Exercise 13.7: -//! What happens when we assign one StrBlob to another? What about StrBlobPtrs? -// In both cases, shallow copy will happen. All pointers point to the same address. -//! -//! Exercise 13.8: -//! Write the assignment operator for the HasPtr class from exercise 13.5 in -//! § 13.1.1 (p. 499). As with the copy constructor, your assignment operator should -//! copy the object to which ps points. -//! - -#include -#include - -class HasPtr -{ -public: - //! default constructor. - HasPtr(const std::string &s = std::string()): - ps(new std::string(s)), i(0) { } - - //! copy constructor. - HasPtr(const HasPtr& hp) : ps(new std::string(*hp.ps)), i(hp.i) { } - - /** - * @brief operator = Copy-Assignment operator - */ - //! return reference to the class - HasPtr& - operator = (const HasPtr& hp) - { //!^^^^^^^^^^^^^^^^^^ right hand side explicit - *ps = *hp.ps; - i = hp.i; - return *this; - //!^^^^^^ return *this . - //! When an operator is a member function, the left-hand operand - //! is bound to the implicit "this" parameter . - } - -private: - std::string *ps; - int i; -}; - -int main() -{ - HasPtr hp("sss"); - HasPtr hp2 = hp; - - - return 0; -} - diff --git a/ch13/README.md b/ch13/README.md index eca62b59..5171b9f7 100644 --- a/ch13/README.md +++ b/ch13/README.md @@ -78,3 +78,35 @@ It is synthesized when the class does not define its own. In both cases, shallow copy will happen. All pointers point to the same address. The `use_count` changed the same as 13.3. ## [Exercise 13.8](ex13_08.h) + +## Exercise 13.9: +>What is a destructor? What does the synthesized destructor do? When is a destructor synthesized? + +The destructor is a member function with the name of the class prefixed by a tilde(~). + +As with the copy constructor and the copy-assignment operator, for some classes, the synthesized destructor is defined to disallow objects of the type from being destoryed. Otherwise, the synthesized destructor has an empty function body. + +The compiler defines a synthesized destructor for any class that does not define its own destructor. + +## Exercise 13.10: +>What happens when a StrBlob object is destroyed? What about a StrBlobPtr? + +When a `StrBlob` object destroyed, the `use_count` of the dynamic object will decrement. It will be freed if no `shared_ptr` to that dynamic object. + +When a `StrBlobPter` object is destroyed the object dynamicaly allocated will not be freed. + +## [Exercise 13.11](ex13_11.h) + +## Exercise 13.12: +>How many destructor calls occur in the following code fragment? +```cpp +bool fcn(const Sales_data *trans, Sales_data accum) +{ + Sales_data item1(*trans), item2(accum); + return item1.isbn() != item2.isbn(); +} +``` + +3 times. There are `accum`, `item1` and `item2`. + +## [Exercise 13.13](ex13_13.cpp) diff --git a/ch13/ex13.7.8.cpp b/ch13/ex13.7.8.cpp deleted file mode 100644 index 436f131f..00000000 --- a/ch13/ex13.7.8.cpp +++ /dev/null @@ -1,74 +0,0 @@ -/*************************************************************************** - * @file main.cpp - * @author Alan.W - * @date 30 DEC 2013 - * @remark - ***************************************************************************/ -//! -//! Exercise 13.6: -//! What is a copy-assignment operator? When is this operator used? What does -//! the synthesized copy-assignment operator do? When is it synthesized? -// A copy-assignment operator is an overloaded operator which controls how -// objects of its class are assigned. -// The compiler generates a synthesized copy-assignment operator if no user- -// defined version is provided. -// Analogously to the copy constructor, for some classes the synthesized copy- -// assignment operator disallows assignment (§ 13.1.6, p. 508). Otherwise, it -// assigns each nonstatic member of the right-hand object to the corresponding -// member of the left-hand object using the copy-assignment operator for the -// type of that member. Array members are assigned by assigning each element -// of the array. The synthesized copy-assignment operator returns a reference -// to its left-hand object. -//! -//! Exercise 13.7: -//! What happens when we assign one StrBlob to another? What about StrBlobPtrs? -// In both cases, shallow copy will happen. All pointers point to the same address. -//! -//! Exercise 13.8: -//! Write the assignment operator for the HasPtr class from exercise 13.5 in -//! § 13.1.1 (p. 499). As with the copy constructor, your assignment operator should -//! copy the object to which ps points. -//! - -#include -#include - -class HasPtr -{ -public: - //! default constructor. - HasPtr(const std::string &s = std::string()): - ps(new std::string(s)), i(0) { } - - //! copy constructor. - HasPtr(const HasPtr& hp) : ps(new std::string(*hp.ps)), i(hp.i) { } - - /** - * @brief operator = Copy-Assignment operator - */ - //! return reference to the class - HasPtr& - operator = (const HasPtr& hp) - { //!^^^^^^^^^^^^^^^^^^ right hand side explicit - *ps = *hp.ps; - i = hp.i; - return *this; - //!^^^^^^ return *this . - //! When an operator is a member function, the left-hand operand - //! is bound to the implicit "this" parameter . - } - -private: - std::string *ps; - int i; -}; - -int main() -{ - HasPtr hp("sss"); - HasPtr hp2 = hp; - - - return 0; -} - diff --git a/ch13/ex13_08.h b/ch13/ex13_08.h index 93ad8004..8d49092b 100644 --- a/ch13/ex13_08.h +++ b/ch13/ex13_08.h @@ -7,9 +7,11 @@ // // Write the assignment operator for the HasPtr class from exercise 13.5 in 13.1.1 (p. 499). // As with the copy constructor, your assignment operator should copy the object to which ps points. +// +// See ex13_05.h -#ifndef CP5_ex13_05_h -#define CP5_ex13_05_h +#ifndef CP5_ex13_08_h +#define CP5_ex13_08_h #include diff --git a/ch13/ex13_11.h b/ch13/ex13_11.h new file mode 100644 index 00000000..c36d4073 --- /dev/null +++ b/ch13/ex13_11.h @@ -0,0 +1,34 @@ +// +// ex13_11.h +// CP5 +// +// Created by pezy on 1/13/15. +// Copyright (c) 2015 pezy. All rights reserved. +// +// Add a destructor to your HasPtr class from the previous exercises. +// +// See ex13_08.h + +#ifndef CP5_ex13_11_h +#define CP5_ex13_11_h + +#include + +class HasPtr { +public: + HasPtr(const std::string &s = std::string()) : ps(new std::string(s)), i(0) { } + HasPtr(const HasPtr &hp) : ps(new std::string(*hp.ps)), i(hp.i) { } + HasPtr& operator=(const HasPtr &hp) { + ps = new std::string(*hp.ps); + i = hp.i; + return *this; + } + ~HasPtr() { + delete ps; + } +private: + std::string *ps; + int i; +}; + +#endif diff --git a/ch13/ex13_13.cpp b/ch13/ex13_13.cpp new file mode 100644 index 00000000..5b23f393 --- /dev/null +++ b/ch13/ex13_13.cpp @@ -0,0 +1,47 @@ +// +// ex13_13.cpp +// CP5 +// +// Created by pezy on 1/13/15. +// Copyright (c) 2015 pezy. All rights reserved. +// +// A good way to understand copy-control members and constructors is to define a simple class with these members in which each member prints its name: +// struct X { +// X() {std::cout << "X()" << std::endl;} +// X(const X&) {std::cout << "X(const X&)" << std::endl;} +// }; +// Add the copy-assignment operator and destructor to X and write a program using X objects in various ways: +// Pass them as nonreference and reference parameters; +// dynamically allocate them; +// put them in containers; and so forth. +// Study the output until you are certain you understand when and why each copy-control member is used. +// As you read the output, remember that the compiler can omit calls to the copy constructor. + +#include +#include +#include + +struct X { + X() { std::cout << "X()" << std::endl; } + X(const X&) { std::cout << "X(const X&)" << std::endl; } + X& operator=(const X&) { std::cout << "X& operator=(const X&)" << std::endl; return *this; } + ~X() { std::cout << "~X()" << std::endl; } +}; + +void f(const X &rx, X x) +{ + std::vector vec; + vec.reserve(2); + vec.push_back(rx); + vec.push_back(x); +} + +int main() +{ + X *px = new X; + f(*px, *px); + delete px; + + return 0; +} +