我想刪除常量字符串的空白空間,但是當我使用刪除函數其顯示錯誤,常量字符串不能修改。請helpQString刪除函數,以刪除常量字符串
const QString abc = "hello world";
QString def = " ";
QString mk = abc .remove(def); // Here error saying const cant change
請幫助
我想刪除常量字符串的空白空間,但是當我使用刪除函數其顯示錯誤,常量字符串不能修改。請helpQString刪除函數,以刪除常量字符串
const QString abc = "hello world";
QString def = " ";
QString mk = abc .remove(def); // Here error saying const cant change
請幫助
您不能修改一個常量字符串。 QString :: remove返回一個對abc的引用,所以刪除作品在abc上,mk不是副本,而是abc!如果你想操作一個const對象,尋找那些是const函數的函數。
編輯:
const QString abc = "hello world";
QString ijk = abc;
QString def = " ";
QString mk = ijk .remove(def); // Here error saying const cant change
相同的操作,無需額外的變量:
const QString abc = "hello world";
QString def = " ";
QString mk = abc;
mk.remove(def);
你不能。你必須創建一個非const的副本。或者首先將字符串設置爲非const。 – 2011-05-27 07:53:35