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?
You can display the name of a variable by using the preprocessor. For instance
outputs
on my machine. The
#changes a token into a string, after preprocessing the line isOf course if you do something like
you will get
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
which gives me
Other compilers may use different naming schemes.