Skip to content

Displaying object name inside destructor

An answer to this question on Stack Overflow.

Question

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

Inside main.cpp

#include"Filetwo.h" 
int main(){
  FileTwo two ;
  return 0;
}

I know this sample program is very small , so we can able to find out the object for which the constructor and destructor has been called . But for big project is there any way to know the object name ? Thanks in advance .

Answer

It is possible. If your compile supports __PRETTY_FUNCTION__ or __func__ (see [this][1]), then you can do this:

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

Note that I've also printed to cerr to ensure that this output gets flushed immediately and isn't lost if the program crashes. Also, since each object has a unique *this pointer, we can use that to see when particular objects are being made or getting killed.

The output for the above program on my computer is:

constructor for FileTwo::FileTwo() at 0x7fff641cde40
Destructor for FileTwo::FileTwo() at 0x7fff641cde40

Note that __func__ is a C99 standard identifier. C++0x adds support in the form of an "implementation-defined string".

__FUNCTION__ is a pre-standard extension supported by some compilers, including Visual C++ (see [documentation][2]) and gcc (see [documentation][3]).

__PRETTY_FUNCION__ is a gcc extension, which does the same sort of stuff, but prettier.

[This question][4] has more information on these identifiers.

Depending on your compiler, this may return the name of the class, though it may be a little mangled.

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

If you are trying to get the name of the variable to which the class is instantiated (two in your case), then there is not, to my knowledge, a way to do this. The following will emulate it:

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

[1]: http://gcc.gnu.org/onlinedocs/gcc/Function-Names.html [2]: http://msdn.microsoft.com/library/b0084kay.aspx [3]: http://gcc.gnu.org/onlinedocs/gcc/Function-Names.html [4]: https://stackoverflow.com/questions/4384765/whats-the-difference-between-pretty-function-function-func