1 | // see also C++ investigation in ~/plg2/cfa2/mycode/string/raii/ctor-calls.cpp
|
---|
2 |
|
---|
3 | #include <string>
|
---|
4 | #include <iostream>
|
---|
5 |
|
---|
6 | using namespace std;
|
---|
7 |
|
---|
8 | #define HELPER_BODY(param) \
|
---|
9 | cout << "early in helper with " << param << endl; \
|
---|
10 | param[0] = '+'; \
|
---|
11 | cout << "late in helper with " << param << endl;
|
---|
12 |
|
---|
13 | void helper1( string q ) { HELPER_BODY(q) }
|
---|
14 | void helper2( string & q ) { HELPER_BODY(q) }
|
---|
15 | #undef HELPER_BODY
|
---|
16 |
|
---|
17 |
|
---|
18 | void calltest_HL() {
|
---|
19 |
|
---|
20 | string fred;
|
---|
21 |
|
---|
22 |
|
---|
23 | cout << "===" << endl;
|
---|
24 | cout << "HL: substring of part" << endl;
|
---|
25 |
|
---|
26 | cout << "---" << endl;
|
---|
27 | // Calling a by-val function, the only way it supports, in which it gets a private logical copy.
|
---|
28 | fred = "abcd";
|
---|
29 | cout << "before helper with " << fred << endl;
|
---|
30 | helper1( fred.substr(1,3) );
|
---|
31 | cout << "after helper with " << fred << endl;
|
---|
32 |
|
---|
33 | cout << "---" << endl;
|
---|
34 | // Calling a by-ref function, catching side effects the only place STL-string gives them, in an explicit copy.
|
---|
35 | fred = "abcd";
|
---|
36 | cout << "before helper with " << fred << endl;
|
---|
37 | {
|
---|
38 | string fred_sub = fred.substr(1,3);
|
---|
39 | helper2( fred_sub );
|
---|
40 | cout << "after helper with temp having " << fred_sub << endl;
|
---|
41 | }
|
---|
42 | cout << "after helper with original having " << fred << endl;
|
---|
43 |
|
---|
44 |
|
---|
45 | cout << "===" << endl;
|
---|
46 | cout << "HL: substring of whole" << endl;
|
---|
47 |
|
---|
48 | cout << "---" << endl;
|
---|
49 | // Calling a by-val function, the only way it supports, in which it gets a private logical copy.
|
---|
50 | fred = "abcd";
|
---|
51 | cout << "before helper with " << fred << endl;
|
---|
52 | helper1( fred.substr(0,4) );
|
---|
53 | cout << "after helper with " << fred << endl;
|
---|
54 |
|
---|
55 | cout << "---" << endl;
|
---|
56 | // Calling a by-ref function, catching side effects the only place STL-string gives them, in an explicit copy.
|
---|
57 | fred = "abcd";
|
---|
58 | cout << "before helper with " << fred << endl;
|
---|
59 | {
|
---|
60 | string fred_sub = fred.substr(0,4);
|
---|
61 | helper2( fred_sub );
|
---|
62 | cout << "after helper with temp having " << fred_sub << endl;
|
---|
63 | }
|
---|
64 | cout << "after helper with original having " << fred << endl;
|
---|
65 |
|
---|
66 |
|
---|
67 |
|
---|
68 | cout << "===" << endl;
|
---|
69 | cout << "HL: whole original string" << endl;
|
---|
70 |
|
---|
71 | cout << "---" << endl;
|
---|
72 | // Calling a by-val function, the only way it supports, in which it gets a private logical copy.
|
---|
73 | fred = "abcd";
|
---|
74 | cout << "before helper with " << fred << endl;
|
---|
75 | helper1( fred );
|
---|
76 | cout << "after helper with " << fred << endl;
|
---|
77 |
|
---|
78 | cout << "---" << endl;
|
---|
79 | // Calling a by-ref function, sys-style, in which we want to gets its changes as side effects.
|
---|
80 | fred = "abcd";
|
---|
81 | cout << "before helper with " << fred << endl;
|
---|
82 | helper2( fred );
|
---|
83 | cout << "after helper with " << fred << endl;
|
---|
84 |
|
---|
85 | cout << "---" << endl;
|
---|
86 | // Calling a by-ref function, trans-style, in which we give it a logical copy, to prevent it from pulluting our gold one; copy needs explicit variable.
|
---|
87 | fred = "abcd";
|
---|
88 | cout << "before helper with " << fred << endl;
|
---|
89 | {
|
---|
90 | string fred_cpy = fred;
|
---|
91 | helper2( fred_cpy );
|
---|
92 | }
|
---|
93 | cout << "after helper with " << fred << endl;
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 |
|
---|
98 | int main() {
|
---|
99 |
|
---|
100 | calltest_HL();
|
---|
101 |
|
---|
102 | }
|
---|