Dis*_*ame 1 c++ string pointers cstring
因此,此代码用于以任何随机顺序输入命令输入,它将返回输入后的值.Amt_Range是一个数字检查功能.
为什么这样做.它应该能够由于指针的比较.
更重要的是string()做了什么.根据我的有限理解,比较不应该起作用,因为交流样式字符串的形式为' - ''t'.提前致谢!!
int main(int argc, const char *argv[]) {
string dummy;
int tests = 0, quizzes = 0, assignments = 0, labs = 0, fin = 0;
int testweight = 0, quizweight = 0, assignweight = 0, labweight = 0, finweight = 0;
int counter = 1;
if (argv[counter] == string("-t")) {
dummy = argv[counter + 1];
tests = Amt_Range(dummy, "tests");
counter+=2;
} else if (argv[counter] == string("-q")) {
dummy = argv[counter + 1];
quizzes = Amt_Range(dummy, "quizzes");
counter+=2;
} else if (argv[counter] == string("-a")) {
dummy = argv[counter + 1];
assignments = Amt_Range(dummy, "assignments");
counter+=2;
} else if (argv[counter] == string("-l")) {
dummy = argv[counter + 1];
labs = Amt_Range(dummy, "labs");
counter+=2;
} else if (argv[counter] == string("-f")) {
dummy = argv[counter + 1];
fin = Amt_Range(dummy, "whether there is a final");
counter+=2;
} else {
cout << "wrong input NOW START OVER" << endl;
exit(EXIT_FAILURE);
}
}
Run Code Online (Sandbox Code Playgroud)
operator==()启动的重载是std命名空间中的自由函数.
namespace std {
bool operator==(std::string const&, std::string const&);
}
Run Code Online (Sandbox Code Playgroud)
它需要第一个参数,const&这意味着欢迎临时.
临时碰巧是使用隐式转换构造函数创建的std::string(char const*).因此,过载适用.
更新 如评论中所揭示的那样,标准实际上已经宣布
Run Code Online (Sandbox Code Playgroud)bool operator==(const char*, std::string const&);作为§21.4.8.2中的优化运算符==.但是,LHS的隐式转换很容易理解,因为它是语言设计中关于运算符重载(分辨率)的关键因素
现在,bool std::operator==(std::string const&, std::string const&)在过载分辨率期间甚至发现过载的原因有点微妙.这种机制称为Argument Dependent Lookup.
在这种情况下,ADL起作用,因为第二个参数已经是a std::string,它具有在std命名空间中声明的类型.因此,将搜索此命名空间以查找候选operator==重载
免责声明:
我简化了以上内容.实际上,标准库中的代码仍然更加通用,可能看起来更像
namespace std {
template <typename Char, typename CharTraits, typename Alloc>
bool operator==(std::basic_string<Char, CharTraits, Alloc> const&, std::basic_string<Char, CharTraits, Alloc> const&) {
// implementation...
}
}
Run Code Online (Sandbox Code Playgroud)