是否可以在多图中返回对象的引用?这就是我正在尝试的:
return &this->noteList.find(key)->second;
但是我得到了Non-const lvalue reference to type 'Note' cannot bind to a temporary of type 'Note *'
所以我想知道它是否可能,如果是的话,怎么样? notelist
是多图,其中包含Note
个对象。
答案 0 :(得分:0)
然后就像其他人已经指出的那样,在没有&符号(second
)的情况下返回&
。
如果noteList
或您的方法为const
,则必须将返回类型更改为const,例如:
const Note &getRef(Note note) const;
关闭主题:通常,您还应将参数更改为const Note ¬e
,并产生:
const Note &getRef(const Note ¬e) const;
或
Note &getRef(const Note ¬e);
答案 1 :(得分:0)
this->noteList.find(key)->second
已经为您提供了对多图中内部对象的引用(如果this->noteList
是多图)。
通过使用&符号(&
)为该表达式添加前缀,您将获得指向此类对象的指针(如果该运算符未过载)