以下是关于const正确性的一个简单问题。
我有这个类:
template <class T>
class Foo
{
public:
std::map<std::string, boost::any> members;
template <typename T>
std::vector<T>& member(const std::string& memberName)
{
return boost::any_cast<std::vector<T>&>(members[memberName]);
}
};
我有一个包括以下内容的函数对象:
bool operator()(Foo& foo) const
{
std::vector<T> & member = foo.member<T>(_memberName);
这里让我困惑的是,我无法将Foo以常量引用的方式传递,因为我调用了非常量成员的getter函数。就其签名而言,这给人的印象是operator()会修改foo。
我应该纠正这个问题吗?如果是,怎么做?
const
引用传递foo
? - Mike Seymour