我有一個非常簡單的方法,並且它的const超載。C++「警告:返回引用臨時」 - 但它不是
Sy_animatable::PropertyTimeLine&
Sy_animatable_imp::getPropertyTimeLine(const QString& property)
{
if (!properties_.contains(property)) {
throw Sy_unknownAnimPropertyException(property);
}
return properties_[property];
}
const Sy_animatable::PropertyTimeLine&
Sy_animatable_imp::getPropertyTimeLine(const QString& property) const
{
if (!properties_.contains(property)) {
throw Sy_unknownAnimPropertyException(property);
}
return properties_[property]; // "warning: returning reference to temporary"
}
我不明白的原因有兩個警告:
properties_
是一個成員變量,它的下標運算符(這是一個QMap
)返回一個參考,所以不應該有任何的臨時並且它在對象的生命週期中持續存在。- 爲什麼警告出現在const超載而不是原始的?
我可以#pragma
該行隱藏警告,但我真的想知道爲什麼它給了我警告 - 有什麼建議嗎?
使用stdlib容器而不是Qt的可憐傳真的另一個原因。 – ildjarn 2012-04-15 17:10:55