当前位置:网站首页>QStringLiteral(str)
QStringLiteral(str)
2022-06-28 21:18:00 【Licht powder】
When looking at the project code , You will always see the following
QString str = QStringLiteral("123rt");QString use QStringLiteral(str) To initialize the , A little curious , Just check it out , Make a note of .
This is the use of QStringLiteral initialization QString Why :
Use QStringLiteral Macros can convert constant strings in code at compile time str Directly constructed as QString object , therefore The runtime does not require additional construction overhead .
Here we add an implicit knowledge sharing :
Implicit sharing runs automatically in the background , So we don't have to write any more code to make this optimization process happen .
Illustrate with examples :
QString srr1 = "happy";
QString str2 = str1;
here QString Two objects of str1 and str2 All point to the same internal data structure in memory . With character data , The data structure holds a Reference count , To indicate how many QString Point to the same data structure . because str1 and str2 All point to the same data , So the value of the reference count is 2
str2[0] = 'D';
When modifying str2 when , It starts with Deep replication of data , In order to ensure that str1 and str2 Point to different data structures , Then the new value is applied to the data he copies . here str1 and str2 The reference count of all becomes 1 了 , The reference count becomes 1 Indicates that the data is not shared .
str.truncate(4);
If you modify it again str2, because str2 Of Data reference count is 1, Data replication will not occur ,truncate() Function directly to str2 Data to operate , String becomes Dappy, The reference count remains 1.
str1 = str2;
When will str2 Assign to str1 when ,str1 Of The data reference count drops to 0, This means that no one QString Still in use “happy” data . This frees the data from memory . Two QString All point to “Dappy”, Now its reference count is 2 了 .
Due to race in reference count , Data sharing is usually used as an option in multithreaded programs without attention .
边栏推荐
- 题解 The SetStack Computer(UVa12096)紫书P116STL的综合应用
- How to open an account in great wisdom? Is it safe
- LeetCode:二叉树展开为链表_114
- LeetCode986. Intersection of interval lists
- LeetCode877. 石子游戏
- LeetCode117. Populate the next right node pointer for each node_ II
- 2. integrate filter
- ID access card copied to mobile phone_ How to turn a mobile phone into an access card mobile NFC copy access card graphic tutorial
- 【筆記:模擬MOS集成電路】帶隙基准(基本原理+電流模+電壓模電路詳解)
- Leetcode daily question - Sword finger offer II 091 Paint the house
猜你喜欢

Automatic operation and maintenance platform based on Apache APIs

基于 Apache APISIX 的自动化运维平台

ThreadLocal principle

如何使用 DataAnt 监控 Apache APISIX

APISIX 助力中东社交软件,实现本地化部署

Leetcode 36. Effective Sudoku (yes, once)
![[learning notes] Introduction to principal component analysis](/img/24/a760d1cd095a967ef258b623eb465c.png)
[learning notes] Introduction to principal component analysis

On the complexity of software development and the way to improve its efficiency

阿里云 MSE 基于 Apache APISIX 的全链路灰度方案实践

Apisik helps Middle East social software realize localized deployment
随机推荐
LeetCode188. The best time to buy and sell stocks IV
[learning notes] cluster analysis
Embedded dynamic Arabic string conversion LCD display string [thanks for Jianguo ambition]
Understand the construction of the entire network model
LeetCode:合并两个有序链表_21
LeetCode每日一题——324. 摆动排序 II
2. integrate filter
[learning notes] Introduction to principal component analysis
APISIX 助力中东社交软件,实现本地化部署
什么是接口?什么是接口测试?
postman简介与安装步骤
Mongodb - replica set and sharding
LeetCode每日一题——710. 黑名单中的随机数
How to add logs to debug anr problems
Leetcode: merge two ordered linked lists_ twenty-one
【Try to Hack】Cobalt Strike(一)
LeetCode213. House raiding II
Leetcode: merge K ascending linked lists_ twenty-three
What is an interface? What is interface testing?
LeetCode123. The best time to buy and sell stocks III