2

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 .

billz
  • 44,644
  • 9
  • 83
  • 100
Viku
  • 2,845
  • 4
  • 35
  • 63

3 Answers3

4

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");
}
Richard
  • 56,349
  • 34
  • 180
  • 251
BЈовић
  • 62,405
  • 41
  • 173
  • 273
4

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;
}
Community
  • 1
  • 1
Richard
  • 56,349
  • 34
  • 180
  • 251
  • What did your answer end up being, @Learner? – Richard Jan 08 '13 at 13:42
  • suppose in one of my function i want to return the function name using _ _FUNCTION__ , then how to do that ? – Viku Jan 08 '13 at 13:44
  • @Richard::not yet get my answer . still searching for it . In your answer i can able to find the function name . but its not telling me which object is calling that function . we can able to get the address of that object . But is there any way to get the name of that object ? – Viku Jan 08 '13 at 13:48
  • @Learner, `__PRETTY_FUNCTION__` seems to do this, in my answer you can see that it prints "FileTwo::FileTwo()", which is the name of the class followed by the name of the function. Does this help? – Richard Jan 08 '13 at 13:52
  • Yes . i got some help from this . In my post constructor has been called since we have declared object two inside main . i want to show this name inside constructor . like whenever we are calling the constructor , two has been displayed on the screen . – Viku Jan 08 '13 at 13:56
  • @Learner, I posted yet another way to get the class name. If you are wanting the name of the variable to which the class is instantiated, I don't know of an easy trick for getting it (sorry!), but I have posted a method which emulates it. – Richard Jan 08 '13 at 14:04
1

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;
}
Ahmed Kato
  • 1,697
  • 4
  • 29
  • 53