只是记录自己学习C++ 笔记实例来自《c++ primer》
1.static:
static 局部对象确保不迟于在程序执行流程第一次经过该对象的定义语句
时进行初始化。这种对象一旦被创建,在程序结束前都不会撤销。当定义静态局
部对象的函数结束时,静态局部对象不会撤销。在该函数被多次调用的过程中,
静态局部对象会持续存在并保持它的值。考虑下面的小例子,这个函数计算了自
己被调用的次数:
创建就不会改变
1 #include <iostream> 2 #include <string.h> 3 #include <string> 4 #include <cmath> 5 6 using namespace std; 7 8 9 size_t count() 10 { 11 static size_t ctr=0; 12 return ++ctr; 13 } 14 15 int main() 16 { 17 for (size_t i=0;i!=10;i++) 18 cout<<count()<<endl; 19 return 0; 20 }
函数重载只能重载 形参不同的函数,只有返回类型不同是错误的。
1 #include <iostream> 2 #include <string.h> 3 #include <string> 4 #include <cmath> 5 6 using namespace std; 7 8 9 int pan(int x) 10 { 11 return x; 12 } 13 14 long long pan(int x,int y) 15 { 16 return x+y; 17 } 18 int main() 19 { 20 int x=6,y=8; 21 cout<<pan(x+y); 22 return 0; 23 }
这样是可以的,
1 #include <iostream> 2 #include <string.h> 3 #include <string> 4 #include <cmath> 5 6 using namespace std; 7 8 9 int pan(int x,int y) 10 { 11 return x; 12 } 13 14 long long pan(int x,int y) 15 { 16 return x+y; 17 } 18 int main() 19 { 20 int x=6,y=8; 21 cout<<pan(x+y); 22 return 0; 23 }
这是错误的
原文:http://www.cnblogs.com/blankvoid/p/5022631.html