【发布时间】:2012-12-22 02:21:04
【问题描述】:
FileTwo.h 内部
#include"iostream"
using namespace std ;
class FileTwo{
public:
FileTwo(){
cout<<"constructor for";//Here want to show the object for which the constructor has been called
}
~Filetwo(){
cout<<"Destructor for ";//Here want to show the object for which the destructor has been called
};
main.cpp 中
#include"Filetwo.h"
int main(){
FileTwo two ;
return 0;
}
我知道这个示例程序非常小,所以我们可以找出调用了构造函数和析构函数的对象。但是对于大项目有没有办法知道对象名称?提前致谢。
【问题讨论】:
-
你说的“名字”是什么意思?变量的标识符?
foo(FileTwo());这样的代码中的“名称”是什么? -
你不能。 c++ 不支持反射。
-
@zapredelom 反射与它无关。对象在 C++ 中没有名称,句号。再多的反思也无法解决这个问题。另一方面,C++ 确实有 RTTI,它会提供类型信息。
-
@Angew::yes 标识符。在这种情况下是两个。
标签: c++ object constructor destructor