Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
muyudou
EffectiveC
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
EffectiveC
/
11.cpp
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
65 lines (55 loc) · 1.59 KB
master
Breadcrumbs
EffectiveC
/
11.cpp
Copy path
Top
File metadata and controls
Code
Blame
65 lines (55 loc) · 1.59 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
//在operator=中处理自我赋值
class Bitmap {};
//保存一个指针指向一块动态分配的bitmap
class Widget
{
public:
//问题的发生:
Widget& operator=(const Widget& rhs)
{
delete pb;
//如果rhs与this指向同一块内存则错误
pb = new Bitmap(*rhs.pb);
return *this;
}
//解决方法1:证同测试,
Widget& operator= (const Widget& rhs)
{
if (this == &rhs)
return *this;
delete pb;
//这里可能出现异常问题,如果new发生异常(不论内存不足还是copy构造函数异常)
//widget会持有一个指针指向一块被删除的bitmap.
pb = new Bitmap(*rhs.pb);
return *this;
}
//解决方法2:精心安排语句顺序来保证“异常安全”,防止解法1的问题
Widget& operator=(const Widget& rhs)
{
//记住原先指针,再构造一个副本,然后再删除,即删除在构造之后
Bitmap* porg = pb;
pb = new Bitmap(*rhs.pb);
delete porg;
return *this;
}
//解法3:2的一个替代方案,即copy and swap技术
void swap(Widget& rhs)
{
//交换数据
}
Widget& operator=(const Widget& rhs)
{
Widget temp(rhs);
swap(temp); //交换*this与temp的数据
return *this;
}
//3的另一个变型解法,依赖以下事实(1) 某类的拷贝赋值操作可能被声明为"by value"方式接受实参
//(2)以by value方式传递东西会造成另一份副本
Widget* operator=(Widget rhs) //这里利用by value构造一个副本
{
swap(rhs); //这里是将*this与副本数据互换,
return *this;
}
private:
Bitmap* pb; //指向一个从堆上分配的对象
};
You can’t perform that action at this time.