我思考了很久,试图寻找一本通在 牛的旅行 这道题目上给出错解的原因。这个寻找的过程也给了我一些启发,在此与各位分享。
继续阅读灯下随笔(五)——一个历史悠久的错误解法给我们的启示分类: 算法
浅谈函数重载规则
引子
昨天在某个群看到了这样一段代码:
#include <iostream>
#include <string>
using namespace std;
void foo(string str) { cout << "string" << endl; }
void foo(bool b) { cout << "bool" << endl; }
void foo(char c) { cout << "char" << endl; }
int main() {
foo("hello, world");
return 0;
}
现在的问题是,这段程序的输出是什么?或者说,foo("hello, world"); 调用的究竟是哪个函数?
要解决这个问题,我们就要详细讨论一下函数重载的规则。
继续阅读浅谈函数重载规则