为了测试__FILE__, __LINE__, __FUNCTION__, 又写了一个C++版本的测试程序,源码如下:
#include <iostream> #include <string> using namespace std; class CUser { private: int m_nId; string m_sName; public: CUser(int id, string name); ~CUser(); void display(); }; CUser::CUser(int id, string name) { m_nId = id; m_sName = name; } CUser::~CUser() { cout<<"Bye-bye, "<<m_nId<<endl; } void CUser::display() { cout<<"File: "<<__FILE__<<", Line: "<<__LINE__<<", Function: "<<__FUNCTION__<<endl; cout<<"id: "<<m_nId<<", name: "<<m_sName<<endl; } int main() { CUser a(1, "Joe Black"); CUser b(2, "Miss Right"); a.display(); b.display(); return 0; }
经过测试:
1. 上面的代码在vc6中编译出错,提示为:error C2065: '__FUNCTION__' : undeclared identifier
2. 上面的代码在vc2010中测试成功,运行结果为:
File: e:\program\vc2010\delme\delme\t.cpp, Line: 32, Function:CUser::display
id: 1, name: Joe Black
File: e:\program\vc2010\delme\delme\t.cpp, Line: 32, Function: CUser::display
id: 2, name: Miss Right
Bye-bye, 2
Bye-bye, 1
请按任意键继续. . .
3. 上面的代码在Fedora下测试也成功了,运行结果为:
File: t.cpp, Line: 32, Function:display
id: 1, name: Joe Black
File: t.cpp, Line: 32, Function: display
id: 2, name: Miss Right
Bye-bye, 2
Bye-bye, 1
注意到结果中的区别了吗?
1) 在VC2010中,__FILE__会把当前文件的全路径都输出来;但是在linux下,只是输出了文件名,不包含路径;
2) 在vc2010中,__FUNCTION__会把类名和函数名都输出来;但是在linux下,只是输出函数名,却不会输出类名。