【发布时间】:2015-05-05 02:02:27
【问题描述】:
拥有这组对象和语句:
QSet<Foo*> set;
iterator QSet::insert(const T & value) //type of the function I want to call
const Foo * get() const //type of the function I use to get the argument
set.insert(get()); //the line showing up as error
我收到错误“没有已知的参数 1 从 'const Foo*' 到 'Foo* const& 的转换”。我想我在阅读这些类型时遇到了麻烦,因为我不知道该怎么做才能完成这项工作。
根据我的阅读, const 关键字适用于其左侧的类型,但顶级 const 除外,它可以写入它所适用的类型的左侧。我的猜测是我必须将 get() 转换为引用,但我不确定该怎么做。
【问题讨论】:
-
这不是“顶级
const”。get()返回指向“const Foo”的指针,而不是const指针。 -
所以我的理解是
const在开头适用于它正确的一切是错误的?那么这样的const到底适用于什么?
标签: c++ pointers reference constants