forked from Mooophy/Cpp-Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
119 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|