我在类中有这样的方法。通过引用传递const值的C++
Word Sentence::parse_word(std::string &word) {
}
一切工作正常。 经过一番考虑后,我得出结论,这是不好的。 因为在这个方法里面,std::string word
没有改变。
所以最好将它作为const std::string &word
来传递,以使方法的使用更加明显和清晰。
而且具有这种签名的方法我使不可能调用它像parse_word(string("some_text))
-
所以我决定改变签名:
Word Sentence::parse_word(const string &word) {
string::iterator iter1= word.begin();
iter1=find(word.begin(),word.end(),'/');
/*some other code */
}
即我不改变这个方法中的那个字符串。
我知道我在这里使用find等方法来接受非包含值,但最好是将string作为const来传递!
,并因为它的怀疑也不能因为它编译:
我不知道,是它在所有好的东西我尝试做?
如何将const字符串转换为字符串? (我尝试使用C风格的转型或const_cast - 没有成功)。
在此先感谢!