重载const操作符[]时使用const_iterator
Using const_iterator when overloading a const operator[]
我真的不明白下面我做错了什么。我有一个名为Settings的类,其结构如下所示,还有下面列出的两个函数。我遇到的问题是,在重载函数中重载const操作符[]时,具体在哪里放置const修饰符。我需要在某处使用const_cast吗?我错过了什么?
class Settings
{
map<string, string> settingsMap;
map<string, string>::const_iterator itc;
const string getValue(string) const;
public:
const string operator[](string) const;
};
const string Settings::operator[](string K) const
{
return getValue(K);
}
const string Settings::getValue(const string K) const
{
const map<string, string> m = settingsMap;
itc = m.begin();
while(itc != m.end())
{
if(itc->first==K)
return itc->second;
itc++;
}
return 0;
}
你的问题不在于const,而在于引用。
此外,类中的声明必须与定义函数时使用的定义完全匹配。
class Settings
{
map<string, string> settingsMap;
map<string, string>::const_iterator itc;
const& string getValue(const string& K) const;
// ^^^^^ Added const to match definition below.
// Also note the reference. This means you are referring
// to the original value but because the reference is const
// it cant be modified via this reference.
// ^^^^^^ This const means the returned value is const.
// unless you return a reference this is meaningless.
// So also note the reference here are well.
// ^^^^^^ The last const means the method will
// NOT change the state of any members.
public:
const string& operator[](const string&) const;
// ^^^^^^^^^^^^^ As above return a const reference.
// ^^^^^ As above pass parameters by const reference
// otherwise it makes a copy of the parameter.
// ^^^^^ function will not change any members.
};
const string& Settings::operator[](const string& K) const // Must match above definition.
{
return getValue(K);
}
const string& Settings::getValue(const string& K) const
{
const map<string, string> m = settingsMap;
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ As this is an object (not a reference)
// You are making a copy of the object into a local variable.
// This is probably not what you want.
const map<string, string>& m = settingsMap; // This works.
itc = m.begin();
while(itc != m.end())
{
if(itc->first==K)
return itc->second;
itc++;
}
return 0;
// ^^^^^^^^ This is not a good idea.
// It is creating a string from a NULL pointer (0 is convertible to pointer).
// What you really wanted to do is return a string. This is also why your
// return type was not working as you expected.
static const string emptyResult; // create a static object to use as the empty result
return emptyResult;
}
如果你的函数getValue()修改了成员itc,那么它不能是const(除非itc是可变的)。我建议您在函数getValue()中声明itc:
...
const map<string, string> m = settingsMap;
imap<string, string>::const_iterator itc = m.begin();
...
操作符[]在c++中有两种含义:
(1) a[i] = x; //store x into a in position i
(2) x = a[i]; //load a in position i to x
第一个必须改变结构…因此结构不能是const
,所以你应该删除最后一个const: const string operator[](const string);
[注意,这里的参数是const string
而不是string
,因为它可能不应该被改变。此外,由于输出字符串可能不应该被更改,因此也应该定义为const
。
第二个,应该可能返回一个string
,而不是const string
,所以你可能应该删除第一个const
: string operator[](const string) const;
[因为这个操作的原始结构没有改变,最后一个const
是好的,应该保留]。另外,注意该参数是const string
而不是string
,因为您不想更改该参数。
在您的情况下:似乎您想要operator[]
的第二个含义,因此您应该将其声明为:string operator[](const string) const;
在你的声明中:
const string operator[](string K) const;
如果你只想读取(并复制)映射的内容,那么正确的声明应该是:
const string operator[](const string &k) const;
返回类型应该是const,否则你可以修改一个临时值,这是不正确的。实参可以通过const引用传递,因为您没有修改它。有了这个声明,下面两行代码会给你一个错误:
...
s["abc"] = "cde"; // cannot modify, the return type is const
s["abc"] += "efg"; // cannot modify, the return type is const
原型声明后的const只是告诉您operator[]
没有修改对象,因此它必须处理方法operator[]
内的代码,并且而不是它的用法。因此,const_iterator
应该被定义为operator[]
代码中的局部变量。
返回类型的const避免了修改临时值(返回类型)的可能性。为了提高效率,您可以通过返回const引用来获得相同的结果。
const string& operator[](const string &k) const;
当然,如果你想用operator[]
修改Settings容器,那么你应该返回一个非const引用。
string& operator[](const string &k);