forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex13_13.cpp
54 lines (49 loc) · 1.37 KB
/
ex13_13.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//
// 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;
}