当前位置:网站首页>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 Blocks Problem(UVa101)紫书P110vector的应用
- LeetCode213. House raiding II
- Proficient in data analysis, double the income? What is the strongest competitiveness
- Understanding of incomplete types
- [learning notes] cluster analysis
- LeetCode877. 石子游戏
- 炒股票能赚钱么?开户安全嘛
- LeetCode226. Flip binary tree
- Apisik helps Middle East social software realize localized deployment
- ANR问题--相机相关的debug
猜你喜欢

Automatic operation and maintenance platform based on Apache APIs
![[Note: analog MOS integrated circuit] bandgap reference (basic principle + current mode + voltage mode circuit explanation)](/img/cd/be62272d465ca990456c222b38df67.png)
[Note: analog MOS integrated circuit] bandgap reference (basic principle + current mode + voltage mode circuit explanation)

Visualization of neural network structure in different frames

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

ThreadLocal principle

接口用例设计

Mongodb - replica set and sharding

API gateway Apache APIs IX helps the evolution of snowball dual active architecture

Bitbucket failed to pull the warehouse Using SSH

Data standardization processing
随机推荐
Bitbucket failed to pull the warehouse Using SSH
The further application of Li Kou tree
Leetcode daily question - Sword finger offer II 091 Paint the house
Flask - Summary
Bitbucket 使用 SSH 拉取仓库失败的问题
【筆記:模擬MOS集成電路】帶隙基准(基本原理+電流模+電壓模電路詳解)
Ref attribute, props configuration, mixin mixing, plug-in, scoped style
Workplace tips | understanding the advantages of the position "knowing people"
Visualization of neural network structure in different frames
LeetCode986. Intersection of interval lists
Which is the most reliable and safe for a securities company to open an account
ANR问题--相机相关的debug
Can you make money by speculating in stocks? It's safe to open an account
题解 Andy s First Dictionary(UVa10815)紫书P112set的应用
LeetCode117. Populate the next right node pointer for each node_ II
How to open an account in great wisdom? Is it safe
Anr analysis - question 1
力扣树的进一步应用
API gateway Apache APIs IX helps the evolution of snowball dual active architecture
rapid ssl通配符证书八百一年是正版吗