Skip to content

Commit

Permalink
added 13.1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Jan 13, 2015
1 parent 60b2afc commit c589f76
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 153 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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)
2 changes: 1 addition & 1 deletion ch07/ex7_41.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
74 changes: 0 additions & 74 deletions ch13/13.6.7.8.cpp

This file was deleted.

32 changes: 32 additions & 0 deletions ch13/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
74 changes: 0 additions & 74 deletions ch13/ex13.7.8.cpp

This file was deleted.

6 changes: 4 additions & 2 deletions ch13/ex13_08.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <string>

Expand Down
34 changes: 34 additions & 0 deletions ch13/ex13_11.h
Original file line number Diff line number Diff line change
@@ -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 <string>

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
47 changes: 47 additions & 0 deletions ch13/ex13_13.cpp
Original file line number Diff line number Diff line change
@@ -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 <iostream>
#include <vector>
#include <initializer_list>

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<X> 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;
}

0 comments on commit c589f76

Please sign in to comment.