为什么Qt QString的替换方法会改变QString?
Why does Qt QString's replace method mutate the QString?
我在当前的Qt代码中使用了许多const QString&
对象。替换字符串的某些部分在不同上下文中的大量QString中很常见。不幸的是,Qt使replace
方法突变了所属的QString(请参阅http://doc.qt.io/qt-5/qstring.html#replace)。
我经常发现自己需要这样做:
// (note that mClass->DType() returns a `const QString&`)
QString modifiedName = mClass->DType();
modifiedName.replace("*", "");
SomeOtherFunction(modifiedName);
而不是这个(AFAIK不会工作(:
// (note that mClass->DType() returns a `const QString&`)
SomeOtherFunction(mClass->DType().replace("*", ""));
有更好的方法吗?为什么QString的replace
方法会改变QString?
将常量分配给非常量对象。就像你可以把一个非常数分配给一个常量一样。有些API不喜欢这样,但Qt并不挑剔。
相关文章:
- 将 Qvector<uint8_t> 转换为 QString
- 变量没有改变?通过向量的函数调用
- 如何改变c++应用程序的视觉效果
- 如何通过按下第三个窗口中的按钮,将QString从一个窗口获取到另一个窗口
- C++在不同线程中改变向量
- 如何访问和改变存储在矢量C++中的对象
- 在C++行尾写一个分号或多个分号是否会改变任何内容
- 为什么字符串的 move() 会改变内存中底层数据的位置?
- 为什么 c++ 动态数组的大小没有改变?
- QStringList vs list<shared_ptr<QString>> 性能比较C++
- 为什么提升图库的 read_graphviz() 函数会改变节点的索引
- 在向量内更改变量的值不会改变其在向量外的值
- 为什么 GCC 不能假设 std::vector::size 在这个循环中不会改变?
- 改变或缩放两个正态分布以具有特定的相关系数
- 将 hh:mm:ss.zzz 时间 QString 转换为毫秒的单行函数?
- 当我使用CHAR_INFO结构时,控制台会无缘无故地改变颜色
- 为什么我的函数接受"std::string"进行排序不会改变它?
- MFC:你能在CDateTimeCtrl中改变自旋的加速度吗?
- 为什么切换 for 循环的顺序会显著改变执行时间?
- 为什么Qt QString的替换方法会改变QString?