在析构函数中显示对象名称

Displaying object name inside destructor

本文关键字:对象 显示 析构函数      更新时间:2023-10-16

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;
}