引用多图对象

时间:2012-11-01 19:53:12

标签: c++ object reference multimap

是否可以在多图中返回对象的引用?这就是我正在尝试的:

return &this->noteList.find(key)->second;

但是我得到了Non-const lvalue reference to type 'Note' cannot bind to a temporary of type 'Note *'所以我想知道它是否可能,如果是的话,怎么样? notelist是多图,其中包含Note个对象。

2 个答案:

答案 0 :(得分:0)

然后就像其他人已经指出的那样,在没有&符号(second)的情况下返回&

如果noteList或您的方法为const,则必须将返回类型更改为const,例如:

const Note &getRef(Note note) const;

关闭主题:通常,您还应将参数更改为const Note &note,并产生:

const Note &getRef(const Note &note) const;

Note &getRef(const Note &note);

答案 1 :(得分:0)

this->noteList.find(key)->second已经为您提供了对多图中内部对象的引用(如果this->noteList是多图)。

通过使用&符号(&)为该表达式添加前缀,您将获得指向此类对象的指针(如果该运算符未过载)