在析构函数中显示对象名称
Displaying object name inside destructor
Inside 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;
}
我知道这个示例程序非常小,所以我们可以找出调用了构造函数和析构函数的对象。但是对于大型项目,有什么方法可以知道对象名称吗?
这是可能的。如果您的编译器支持__PRETTY_FUNCTION__
或__func__
(参见此),那么您可以这样做:
#include <iostream>
using namespace std;
class FileTwo{
public:
FileTwo(){
cerr<<"constructor for "<< __PRETTY_FUNCTION__ <<" at "<<&(*this)<<endl;
}
~FileTwo(){
cerr<<"Destructor for "<< __PRETTY_FUNCTION__ <<" at "<<&(*this)<<endl;
}
};
int main(){
FileTwo two;
return 0;
}
请注意,我还打印到cerr
,以确保该输出立即刷新,并且在程序崩溃时不会丢失。此外,由于每个对象都有一个唯一的*this
指针,我们可以使用它来查看特定对象何时被创建或被终止。
以上程序在我的计算机上的输出是:
constructor for FileTwo::FileTwo() at 0x7fff641cde40
Destructor for FileTwo::FileTwo() at 0x7fff641cde40
注意__func__
是C99标准标识符。c++ 0x以"实现定义的字符串"的形式增加了支持。
__FUNCTION__
是一些编译器支持的预标准扩展,包括Visual c++(参见文档)和gcc(参见文档)。
__PRETTY_FUNCION__
是一个gcc扩展,做同样的事情,但更漂亮。
这个问题有更多关于这些标识符的信息。
根据编译器的不同,这可能返回类的名称,尽管它可能有点混乱。
#include <iostream>
#include <typeinfo>
using namespace std;
class FileTwo{
public:
FileTwo(){
cerr<<"constructor for "<< typeid(*this).name() <<" at "<<&(*this)<<endl;
}
~FileTwo(){
cerr<<"Destructor for "<< typeid(*this).name() <<" at "<<&(*this)<<endl;
}
};
int main(){
FileTwo two;
return 0;
}
如果您试图获得类实例化的变量的名称(在您的情况下为two
),那么据我所知,没有办法做到这一点。下面将模拟它:
#include <iostream>
#include <string>
using namespace std;
class FileTwo{
public:
FileTwo(const std::string &myName) : myName(myName) {
cerr<<"constructor for "<< myName <<" at "<<&(*this)<<endl;
}
~FileTwo(){
cerr<<"Destructor for "<< myName <<" at "<<&(*this)<<endl;
}
private:
std::string myName;
};
int main(){
FileTwo two("two");
return 0;
}
除非您命名对象,否则是不可能的。像这样:
#include <iostream>
#include <string>
using namespace std;
class FileTwo {
public:
FileTwo(const std::string &myName) : name(myName){
cout<<"constructor for" << name;//Here want to show the object for which the constructor has been called
}
~Filetwo(){
cout<<"Destructor for " << name;//Here want to show the object for which the destructor has been called
}
private:
std::string name;
};
,然后将main改为:
#include"Filetwo.h"
int main(){
FileTwo two("two 11");
}
不可能给对象命名,你所能做的就是创建一个私有变量来保存这个名字。
using namespace std;
class myClass
{
private:
string className;
public:
~myClass()
{
cout<<this->className;
}
};
你可以为你的变量创建setter和getter。
void SetName(string name)
{
this->className = name;
}
string GetName()
{
return this->className;
}
相关文章:
- 对象初始化后在C++中显示 char 数组时的异常行为
- 为什么静态数组成员变量在调用对象的实例后不显示任何内容?
- 使用 CIN COUT 显示垃圾值的文件对象?
- IM 试图显示对象数组,但它给出了错误的输出
- 对象错误地显示为引用?
- C++ 矢量不显示包含对象的数据
- C++ csv 文件无法正确加载到矢量,它似乎知道对象在那里但不显示它们
- 如何在屏幕上显示对象列表?
- 为什么谷歌测试/模拟显示 std::unique_ptr 泄露的模拟对象错误?
- 是否有一个函数可以显示我创建的类中的对象到 QTextBrowser 中?
- 在同一屏幕上显示两个不同的对象
- 为什么析构函数和复制构造函数使用对象数组显示此行为
- 使用OpenGL显示两个对象.纹理不像预期的那样行为
- 如何以某种格式为用户显示对象向量
- 使用 main() 以外的其他函数显示类的对象,但在 main() 中调用;
- 如何使 UI 对象仅在单击按钮后显示在对话框中
- 当从此到子类中的新对象时,将显示警告"destination for this 'memcpy' call is a pointer to dynamic class..."
- 在 opengl 中应用转换函数后未显示对象
- Mac上的选定对象显示背景高亮显示,而不是边框高亮显示
- 如何在c++中使用单链表对象显示和添加数据