Viku
Viku

Reputation: 2973

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

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 .

Upvotes: 2

Views: 2153

Answers (3)

Richard
Richard

Reputation: 61459

It is possible. If your compile supports __PRETTY_FUNCTION__ or __func__ (see this), 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) and gcc (see documentation).

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

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

Upvotes: 4

BЈовић
BЈовић

Reputation: 64273

Unless you name the object, it is not possible. Something like this :

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

and then change the main into :

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

Upvotes: 4

Ahmed Kato
Ahmed Kato

Reputation: 1707

It is not possible to name the object,all what you can do is making a private variable to hold the name.

using namespace std;
class myClass
{
    private:
    string className;

    public:
    ~myClass()
    {
        cout<<this->className;
    }
};

you can create setters and getters for you variable.

void SetName(string name)
{
   this->className = name;
}

string GetName()
{
   return this->className;
}

Upvotes: 1

Related Questions