C++拾遗--函数重载
函数重载似乎并不难,但仍有一些问题值得注意。下面从几个关键词入手:
关于作用域,需要指出几点事实
#include <iostream> using namespace std; //位于全局作用域的var int var = 0; void print(void) { cout << "全局print" << endl; } int main() { //位于局部作用域的g_var会隐藏位于全局作用域的g_var int var = 1; //局部print同样会隐藏全局print auto print = [](void){ cout << "局部print" << endl; }; //以下调用全为局部的 cout << "var = " << var << endl; print(); //调用全局的 cout << "::var = " << ::var << endl; ::print(); cin.get(); return 0; }运行
#include <iostream> using namespace std; void print(const char *str) { cout << "void print(const char *str) call" << endl; cout << str; } void print(char *str) { cout << "void print(char *str) call" << endl; cout << str; } int main() { char str[] = "David"; print("zhangxiang"); cout << endl; print(str); cin.get(); return 0; }运行
原文地址:http://blog.csdn.net/zhangxiangdavaid/article/details/44039445