《2021年经典c++面试题(3).pdf》由会员分享,可在线阅读,更多相关《2021年经典c++面试题(3).pdf(13页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、1 经典 c+面试题(3)9.结构与联合有和区别1.结构和联合都是由多个不同的数据类型成员组成,但在任何同一时刻,联合中只存放了一个被选中的成员(所有成员共用一块地址空间),而结构的所有成员都存在(不同成员的存放地址不同)。2.对于联合的不同成员赋值,将会对其它成员重写,原来成员的值就不存在了,而对于结构的不同成员赋值是互不影响的。10.下面关于“联合”的题目的输出a)#include union int i;char x2;void main()a.x0=10;a.x1=1;printf(%d,a.i);精品w o r d 学习资料 可编辑资料-精心整理-欢迎下载-第 1 页,共 13 页2
2、 答案:266(低位低地址,高位高地址,内存占用情况是Ox010A)b)main()union int i;struct char first;char second;half;number;number.i=0 4241;printf(%c%c n,number.half.first,mumber.half.second);number.half.first=a number.half.second=b printf(%x n,number.i);getch();答案:AB(0 41 对应 A,是低位;Ox42 对应 B,是高位)6261(number.i和 number.half共用一块地
3、址空间)11.已知strcpy的函数原型:char*strcpy(char*strDest,精品w o r d 学习资料 可编辑资料-精心整理-欢迎下载-第 2 页,共 13 页文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文
4、档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ
5、4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10
6、J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 H
7、S4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X
8、5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4
9、ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A
10、8Y6F33 const char*strSrc)其中 strDest 是目的字符串,strSrc 是源字符串。不调用 C+/C 的字符串库函数,请编写函数strcpy。答案:char*strcpy(char*strDest,const char*strSrc)if(strDest=NULL|strSrc=NULL)return NULL;if(strDest=strSrc)return strDest;char*tempptr=strDest;while(*strDest+=*strSrc+)!=0);return tempptr;12.已知 String 类定义如下:class Strin
11、g public:String(const char*str=NULL);/通用构造函数String(const String another);/拷贝构造函数 String();/析构函数String operater=(const String rhs);/赋值函数精品w o r d 学习资料 可编辑资料-精心整理-欢迎下载-第 3 页,共 13 页文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8
12、Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档
13、编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4
14、P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J
15、6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS
16、4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5
17、U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 Z
18、G9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F34 private:char*m_data;/用于保存字符串尝试写出类的成员函数实现。答案:String:String(const char*str)if(str=NULL)/strlen在参数为 NULL 时会抛异常才会有这步判断m_data=new char1;m_data0=0;else m_data=new charstrlen(str)+1;strcpy(m_data,str);String:Str
19、ing(const String another)m_data=new charstrlen(another.m_data)+1;精品w o r d 学习资料 可编辑资料-精心整理-欢迎下载-第 4 页,共 13 页文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10
20、X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4
21、 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3
22、A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3
23、文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:C
24、J4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S1
25、0J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7
26、HS4Z10X5U4X4 ZG9R3A8Y6F35 strcpy(m_data,other.m_data);String String:operator=(const String rhs)if(this=rhs)return*this;delete m_data;/删除原来的数据,新开一块内存m_data=new charstrlen(rhs.m_data)+1;strcpy(m_data,rhs.m_data);return*this;String:String()delete m_data;13.h 头文件中的 ifndef/define/endif 的作用答:防止该头文件被重复引用。1
27、4.#include 与#include file.h 的区别答:前者是从 Standard Library的路径寻找和引用file.h,而后者是从当前工作路径搜寻并引用file.h。精品w o r d 学习资料 可编辑资料-精心整理-欢迎下载-第 5 页,共 13 页文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y
28、6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编
29、码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P
30、9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6
31、I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4
32、Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U
33、4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG
34、9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F36 15.在 C+程序中调用被 C 编译器编译后的函数,为什么要加extern“C”首先,作为 extern 是 C/C+语言中表明函数和全局变量作用范围(可见性)的关键字,该关键字告诉编译器,其声明的函数和变量可以在本模块或其它模块中使用。通常,在模块的头文件中对本模块提供给其它模块引用的函数和全局变量以关键字extern 声明。例如,如果模块B 欲引用该模块 A中定义的全局变量和函数时只需包含模块A 的头文件即可。这样,模块 B 中调用模块 A 中的函数时,在编译阶段,模块B 虽然找不到该
35、函数,但是并不会报错;它会在连接阶段中从模块 A 编译生成的目标代码中找到此函数extern C 是连接申明(linkage declaration),被 extern C 修饰的变量和函数是按照C 语言方式编译和连接的,来看看 C+中对类似C 的函数是怎样编译的:作为一种面向对象的语言,C+支持函数重载,而过程式语言 C则不支持。函数被C+编译后在符号库中的名字与C 语言的不同。例如,假设某个函数的原型为:void foo(int x,int y);该函数被 C 编译器编译后在符号库中的名字为_foo,而 C+编译器则会产生像 _foo_int_int之类的名字(不同的编译器可能生成的 名
36、字 不同,但 是都 采 用 了相 同 的 机制,生 成的新 名 字称 为“mangled name”)。精品w o r d 学习资料 可编辑资料-精心整理-欢迎下载-第 6 页,共 13 页文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A
37、8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文
38、档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ
39、4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10
40、J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 H
41、S4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X
42、5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4
43、ZG9R3A8Y6F37 _foo_int_int这样的名字包含了函数名、函数参数数量及类型信息,C+就是靠这种机制来实现函数重载的。例如,在C+中,函数 void foo(int x,int y)与 void foo(int x,float y)编译生成的符号是不相同的,后者为_foo_int_float。同样地,C+中的变量除支持局部变量外,还支持类成员变量和全局变量。用户所编写程序的类成员变量可能与全局变量同名,我们以.来区分。而 本质上,编译器在进行编译时,与函数的处理相似,也为类中的变量取了一个独一无二的名字,这个名字与用户程序中同名的全局变量名字不同。未加 extern C 声明时
44、的连接方式假设在 C+中,模块 A 的头文件如下:/模块 A 头文件moduleA.h#ifndef MODULE_A_H#define MODULE_A_H int foo(int x,int y);#endif 在模块 B 中引用该函数:/模块 B 实现文件moduleB.cpp#include moduleA.h foo(2,3);实际上,在连接阶段,连接器会从模块A 生成的目标文件moduleA.obj中寻找 _foo_int_int这样的符号!精品w o r d 学习资料 可编辑资料-精心整理-欢迎下载-第 7 页,共 13 页文档编码:CJ4P9S10J6I7 HS4Z10X5U4
45、X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9
46、R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6
47、F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码
48、:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9
49、S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I
50、7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z10X5U4X4 ZG9R3A8Y6F3文档编码:CJ4P9S10J6I7 HS4Z