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 1055837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:37:29+00:00 2026-05-16T17:37:29+00:00

Is it possible to get the object name too? #include<cstdio> class one { public:

  • 0

Is it possible to get the object name too?

#include<cstdio>

class one {
public:
    int no_of_students;
    one() { no_of_students = 0; }
    void new_admission() { no_of_students++; }
};

int main() {
    one A;
    for(int i = 0; i < 99; i++) {
        A.new_admission();
    }
    cout<<"class"<<[classname]<<" "<<[objectname]<<"has "
        <<A.no_of_students<<" students";
}

where I can fetch the names, something like

[classname] = A.classname() = one
[objectname] = A.objectname() = A

Does C++ provide any mechanism to achieve this?

  • 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-05-16T17:37:30+00:00Added an answer on May 16, 2026 at 5:37 pm

    You can display the name of a variable by using the preprocessor. For instance

    #include <iostream>
    #define quote(x) #x
    class one {};
    int main(){
        one A;
        std::cout<<typeid(A).name()<<"\t"<< quote(A) <<"\n";
        return 0;
    }
    

    outputs

    3one    A
    

    on my machine. The # changes a token into a string, after preprocessing the line is

    std::cout<<typeid(A).name()<<"\t"<< "A" <<"\n";
    

    Of course if you do something like

    void foo(one B){
        std::cout<<typeid(B).name()<<"\t"<< quote(B) <<"\n";
    }
    int main(){
        one A;
        foo(A);
        return 0;
    }
    

    you will get

    3one B
    

    as the compiler doesn’t keep track of all of the variable’s names.

    As it happens in gcc the result of typeid().name() is the mangled class name, to get the demangled version use

    #include <iostream>
    #include <cxxabi.h>
    #define quote(x) #x
    template <typename foo,typename bar> class one{ };
    int main(){
        one<int,one<double, int> > A;
        int status;
        char * demangled = abi::__cxa_demangle(typeid(A).name(),0,0,&status);
        std::cout<<demangled<<"\t"<< quote(A) <<"\n";
        free(demangled);
        return 0;
    }
    

    which gives me

    one<int, one<double, int> > A
    

    Other compilers may use different naming schemes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.