-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathclass_copy_assignment.cpp
More file actions
40 lines (35 loc) · 1.12 KB
/
class_copy_assignment.cpp
File metadata and controls
40 lines (35 loc) · 1.12 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
#include <iostream>
class mystring {
public:
mystring() {
std::cout << "mystring()" << std::endl;
}
/* explicit */ mystring(const char *s)
{
std::cout << "mystring(const char *s)" << std::endl;
}
mystring(const mystring &rlh) {
std::cout << "mystring(const mystring &rlh)" << std::endl;
}
//需要明白的是:虽然下面的直接赋值的构建方式并没有调用拷贝构造函数
//(这是编译器的优化)但,依然需要此拷贝构造函数可见,定义为 delete
//将无法通过编译
//mystring(const mystring &rlh) = delete;
mystring& operator=(const mystring &rlh)
{
std::cout << "operator=(const mystring &rlh)" << std::endl;
return *this;
}
};
int main(int argc, const char *argv[])
{
std::cout << "~~~~~~~~~~~~~~~~~" << std::endl;
mystring mys = "mys";
std::cout << "~~~~~~~~~~~~~~~~~" << std::endl;
mys = "MYS";
std::cout << "~~~~~~~~~~~~~~~~~" << std::endl;
mystring mys2 = mystring("mys2");
std::cout << "~~~~~~~~~~~~~~~~~" << std::endl;
mystring mys3 = {"mys3"};
return 0;
}