-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdraft00.cpp
More file actions
52 lines (49 loc) · 1.11 KB
/
Copy pathdraft00.cpp
File metadata and controls
52 lines (49 loc) · 1.11 KB
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
#include <iostream>
class MyClass
{
public:
double* data = nullptr;
MyClass(double x)
{
std::cout << "call MyClass(double)" << std::endl;
data = new double[100];
data[0] = x;
}
MyClass(const MyClass &old): data(old.data)
{
std::cout << "call MyClass(&)" << std::endl;
}
MyClass(MyClass &&old): data(old.data){
old.data = nullptr;
std::cout << "call MyClass(&&)" << std::endl;
}
~MyClass()
{
if (data)
{
std::cout << "call ~MyClass()" << std::endl;
delete[] data;
}
}
};
MyClass some_function(bool test)
{
std::cout << "call some_function()" << std::endl;
MyClass tmp0(2.33), tmp1(4.66);
std::cout << "start return MyClass" << std::endl;
if (test){
return tmp0;
}else
{
return tmp1;
}
}
// g++ draft00.cpp -std=c++11 -o tbd00.exe
int main()
{
std::cout << "call main()" << std::endl;
MyClass tmp0 = some_function(true);
std::cout << "bad value" << tmp0.data[0] << std::endl;
std::cout << "end of main()" << std::endl;
return 0;
}