当前位置:网站首页>QStringLiteral(str)
QStringLiteral(str)
2022-06-28 21:08:00 【Licht小粉】
在看项目代码的时候,总会看到下面这种情况
QString str = QStringLiteral("123rt");QString用QStringLiteral(str)来初始化,有点好奇,就查了下,记录一下。
这是用QStringLiteral初始化QString的原因:
使用 QStringLiteral 宏可以在编译期把代码里的常量字符串 str 直接构造为 QString 对象,于是运行时就不再需要额外的构造开销了。
这里补充一个知识隐含共享:
隐含共享在后台自动运行,所以我们不用再编写任何代码来促使这个优化过程发生。
举例说明:
QString srr1 = "happy";
QString str2 = str1;
此时QString的两个对象str1和str2都指向内存中相同的内部数据结构。与字符数据一起,数据结构保存一个引用计数,以指出有多少QString指向相同的数据结构。因为str1和str2都指向相同的数据,所以引用计数的值为2
str2[0] = 'D';
当修改str2时,它首先对数据进行深层复制,以确保str1和str2指向不同的数据结构,然后才将新数值应用于他复制的数据。此时str1和str2的引用计数都变成1了,引用计数变成1表示数据并未被共享。
str.truncate(4);
如果再次修改str2,由于str2的数据引用计数为1,将不会发生数据复制,truncate()函数直接对str2的数据进行操作,字符串变成Dappy,引用计数保持为1。
str1 = str2;
当将str2赋给str1时,str1的数据引用计数降为0,这意味着没有一个QString仍在使用“happy”数据。这样数据就从内存中释放了。两个QString都指向“Dappy”,现在它的引用计数就是2了。
由于引用计数中的竞争情况,数据共享在多线程程序中通常只是作为一个选项而没有给与关注。
边栏推荐
- LeetCode每日一题——30. 串联所有单词的子串
- 炒股票能赚钱么?开户安全嘛
- Bitbucket failed to pull the warehouse Using SSH
- Leetcode daily question - 324 Swing sort II
- LeetCode560. Subarray with and K
- T-test (test whether the mean difference between the two populations is significant)
- Please allow the "imperfection" of the current domestic Tob
- How to do a good job in customer's successful bottom design | tob Master Course
- Resilience4j retry source code analysis and retry index collection
- LeetCode560. 和为K的子数组
猜你喜欢

Pyechart drawing multiple Y-axis line graphs

【笔记:模拟MOS集成电路】带隙基准(基本原理+电流模+电压模电路详解)

How to analyze the relationship between enterprise digital transformation and data asset management?

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

mysql-发生系统错误1067

不同框架的绘制神经网络结构可视化

视频号如何下载视频?来看超简单方法!

我也差点“跑路”

接口用例设计

MongoDB——副本集与分片
随机推荐
不同框架的绘制神经网络结构可视化
LeetCode986. Intersection of interval lists
LeetCode560. 和为K的子数组
大智慧上怎么进行开户啊, 安全吗
Embedded dynamic Arabic string conversion LCD display string [thanks for Jianguo ambition]
Leetcode daily question - 30 Concatenate substrings of all words
穩定性總結
[learning notes] Introduction to principal component analysis
On the complexity of software development and the way to improve its efficiency
LeetCode986. 区间列表的交集
Anr problem - camera related debug
LeetCode560. Subarray with and K
APISIX 助力中东社交软件,实现本地化部署
【读书会第13期】视频文件的封装格式
开通挖财账号安全吗?是靠谱的吗?
How to analyze the relationship between enterprise digital transformation and data asset management?
I almost ran away
LeetCode121. 买卖股票的最佳时机
题解 Pie(POJ3122)超详细易懂的二分入门
No module named ‘PyEMD‘ ;使用plt.figure()TypeError: ‘module‘ object is not callable