头文件中只存放“声明”而不存放“定义”

头文件中只存放“声明”而不存放“定义” 在 C++ 语法中,类的成员函数可以在声明的同时被定义,并且自动成为内联函数 。

这虽然会带来书写上的方便,但却造成了风格不一致,弊大于利。建议将成员函数的定 义与声明分开,不论该函数体有多么小。

 1 #include <iostream>
 2 
 3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 4 using namespace std;
 5 //计算字符串长度的函数
 6 int str_len(const char *string)
 7 {
 8     //char *temp=string; 编译报错!
 9     //*string='x';       编译报错!
10     int i=0;
11     while (*(string+i)!=NULL) 
12         i++;
13     return i;
14 }
15 //main()函数中测试str_len()
16 int main(int argc, char** argv) {
17     char a[]="ABCDE";
18     cout<<a<<"\t"<<str_len(a)<<endl;
19     char *str="Hello!";
20     cout<<str<<"\t"<<str_len(str)<<endl;
21     cout<<"This is a test."<<"\t"<<str_len("This is a test.")<<endl;
22     return 0;
23 }

 

posted @ 2018-08-03 11:01  borter  阅读(478)  评论(0编辑  收藏  举报