显示当前行号、文件名和函数名(二)
2011-10-21 14:18
316 查看
为了测试__FILE__, __LINE__, __FUNCTION__, 又写了一个C++版本的测试程序,源码如下:
经过测试:
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下,只是输出函数名,却不会输出类名。
#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下,只是输出函数名,却不会输出类名。
相关文章推荐
- 显示当前行号、文件名和函数名
- python 获取当前行号 函数名 文件名
- C++打印当前文件名,行号,当前函数名
- 【python系列】python 获取当前位置所在的文件名、函数名和行号
- Python 中如何获取当前位置所在的文件名,函数名,以及行号
- linux编程中显示文件名,函数名,行号
- c#获取当前代码运行的文件名、运行的函数名以及当前代码执行的行号
- 在sh 脚本中得到sh的文件名和路径的函数,如需要用到当前脚本路径时
- 在vim中显示当前正在编辑的文件名(包括路径)
- 当前表格中鼠标悬停时改变显示风格函数
- C/C++ 打印文件名、行号、函数名的方法
- python 获取当前位置所在的函数名和行号
- C# 里怎样得到当前执行的函数名,当前代码行,源代码文件名。
- C#获得当前执行的函数名、当前代码行、源代码文件名
- golang 获取当前文件名和行号的方法
- python语言 python打印当前函数名称和行号示例
- python中获取当前位置所在的行号和函数名
- 日志中自动记录所在函数名、文件名、行号
- Java打印文件名、函数名、行号
- PL/SQL显示行号和高亮当前行