Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9094633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:23:42+00:00 2026-06-16T23:23:42+00:00

Inside FileTwo.h #includeiostream using namespace std ; class FileTwo{ public: FileTwo(){ cout<<constructor for;//Here want

  • 0

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 .

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T23:23:43+00:00Added an answer on June 16, 2026 at 11:23 pm

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Inside FileTwo.h #ifndef FILETWO #define FILETWO #includeFileOne.h class FileOne ; class FileTwo { public:
Inside FileThree.h #ifndef FILETHREE #define FILETHREE namespace blue{} class Filethree { public: Filethree(void); ~Filethree(void);
Inside public class IAmHere extends Activity implements LocationListener { i have @Override public void
Inside of the onReceive(Content context, Intent intent) method of my public class MediaButtonIntentReceiver extends
Inside a repeater's ItemTemplate there is a: <tr class=class1> </tr> I want this class
Inside Myclass.h Class Myclass { public: Myclass(); private: static int Myarray[12]; }; How to
Inside the Person Class: public Point[] endPointArray; private int scalar; private Head head; private
Inside admin I want the list_display of a model to include the get_absolute_url and
Inside the LAN, we are good to go… folks access our intranet using http://
inside show.js.erb $('#viewport').html(<%= escape_javascript(render('show')) %>); I am trying to render the show view into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.