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
Events
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
/
14.cpp
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
52 lines (42 loc) · 1.53 KB
master
Breadcrumbs
EffectiveC
/
14.cpp
Copy path
Top
File metadata and controls
Code
Blame
52 lines (42 loc) · 1.53 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
//资源管理类的copying行为
class Lock
{
public:
explicit Lock(Mutex* pm) : mutexPtr(pm)
{
lock(mutexPtr); //构造函数锁住资源
}
~Lock()
{
unlock(mutexPtr); //析构函数释放资源
}
};
//客户端的用法
Mutex m;
Lock ml(&m); //锁定,执行关键区域内的操作
...
//如果进行拷贝,可以发生以下情况:
Lock ml1(&m);
Lock ml2(ml1);
//情况1:禁止复制,参考条款6的做法,
//1声明为私有,不定义,如果复制发生链接错误
//2建一个不能拷贝的基类,并私有继承于它,这将错误移到编译期
class Lock : private Uncopyable
{
};
//情况2:引用计数法,例如shared_ptr的行为,但当资源变为0时,它的默认行为是
//删除所指物,而如果我们只是释放锁定,这种情况下可以利用shared_ptr中可以指定“删除器”
//的一个函数或函数对象,当引用计数为0时,调用此函数,这个参数对它的构造函数是可有可无的第二个参数
class Lock
{
public:
explicit Lock(Mutex* pm) : mutexPtr(pm, unlock) //unlock即为指定的删除器
{
lock(mutexPtr.get());
}
private:
std::tr1::shared_ptr<Mutex> mutexPtr; //用shared_ptr管理这个对象,并且指定删除器,自定义行为
};
//而且以上不再需要析够函数,因为析构函数会在引用计数为0时自动调用shared_ptr的删除器
//情况3:复制底部资源,进行深度拷贝,例如string类,
//情况4:转移底部所有权,如auto_ptr的行为,资源所有权从被复制物转移到目标物
You can’t perform that action at this time.