【发布时间】:2018-08-22 15:28:20
【问题描述】:
我尝试从没有const 项目的列表中抑制 const 项目指针:
#include <QString>
#include <QList>
class Item {
public:
Item(QString name) : _name(name) {
}
private:
QString _name;
};
class Product {
public:
Product(const Item * item) : _item(item) {
}
const Item * item() {
return _item;
}
void setItem(const Item * item) {
_item = item;
}
private:
const Item * _item;
};
int main() {
QList<Item*> listOfNonConstItem;
listOfNonConstItem.append(new Item("a"));
listOfNonConstItem.append(new Item("b"));
Product product(listOfNonConstItem.first());
listOfNonConstItem.removeOne(product.item());
return 0;
}
很遗憾,编译失败:
/path/to/main.cpp:34:31: error: reference to type 'Item *const' could not bind to an lvalue of type 'const Item *'
editableList.removeOne(pC);
^~
/path/to/Qt/5.6.3/clang_64/lib/QtCore.framework/Headers/qlist.h:201:29: note: passing argument to parameter 't' here
bool removeOne(const T &t);
^
我不明白为什么会出现问题,因为removeOne()参数是const。
【问题讨论】:
-
不要将指针与指针混淆。
-
pointee 是什么意思?
-
指向到的对象类型。在指针类型中表示为星号
*之前的任何内容。 -
@LogicStuff 我刚刚编辑了我的示例,所以行号匹配
-
顺便说一句,在有人帮助您解决原始问题之后,在您的帖子中添加一个完全独立的问题(您甚至承认原始问题已通过下面的答案解决),这是不好的 Stack Overflow 礼仪.我建议您恢复编辑,接受答案,然后发布一个新问题。
标签: c++ list qt pointers constants