forked from Mooophy/Cpp-Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex13.14.15.16.cpp
86 lines (77 loc) · 1.97 KB
/
ex13.14.15.16.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/***************************************************************************
* @file main.cpp
* @author Alan.W
* @date 31 DEC 2013
* @remark
***************************************************************************/
//! Exercise 13.14:
//! Assume that numbered is a class with a default constructor that generates
//! a unique serial number for each object, which is stored in a data member
//! named mysn. Assuming numbered uses the synthesized copy-control members and
//! given the following function:
//!
// void f (numbered s) { cout << s.mysn << endl; }
//! what output does the following code produce?
// numbered a, b = a, c = b;
// f(a); f(b); f(c);
//! x
//! x
//! x
//! i.e.three identical numbers. --correct!
//!
//! Exercise 13.15:
//! Assume numbered has a copy constructor that generates a new serial number.
//! Does that change the output of the calls in the previous exercise?
//! If so, why? What output gets generated?
//! Yes. the ouput will change.
//! 102
//! 103
//! 104
//! --correct!
//!
//! Exercise 13.16:
//! What if the parameter in f were const numbered&? Does that change the
//! output? If so, why? What output gets generated?
//!
//! Yes. Because ,if so, no copy operation any more.The function f just print
//! the object of Numbered directly whose mySn is unique. But if without the newly
//! defined copy constructor , it will still be :
//! 1
//! 1
//! 1.
#include <string>
#include <iostream>
#include <vector>
#include <memory>
struct Numbered
{
//! for ex13.14
Numbered()
{
static unsigned i = 0;
++i;
mySn = i;
}
//! for ex13.15
/*
Numbered(const Numbered& num)
{
static unsigned j = 99;
++j;
mySn = j;
}
*/
unsigned mySn;
};
//void f (Numbered s)
//!
void f(Numbered& s)
{
std::cout << s.mySn << std::endl;
}
int main()
{
Numbered a, b = a, c = b;
f(a); f(b); f(c);
return 0;
}