ive been looking back on some old programs ive done in the past to see if i could write a more efficient version implementing new coding techniques and whatnot ive learned since then. so heres a fairly stupid question:
is there a way to retrieve the name of an object?
(better if i showed an example)
suppose i had the following code:
#include <iostream>
using namespace std;
class Object
{
public:
Object(int n = 0) : number(n)
{
cout << "This Object, " << (((the name))) << " has " << n << endl; //replace (((the name))) with "the name"
}
private:
int number;
};
int main(int argc, char *argv[])
{
Object MyObjectName(24);
return 0;
}
My goal is to have (((the name))) be replaced by the actual name (for logging purposes) so that when i run it, i get this:
This Object, MyObjectName has 24
(forgive me for not using correct terminology whenever it applies)
My C++ is a little rusty, but try doing a typeof on the object. If this was .Net it would be easy, you could just do a object.ToString().
Objects don't strictly have names (as given by the compiler.)
If you need such a facility, then you have to write it into each the class eg. a getName() function.
I might have an answer depending on which compiler you're using.
_Some_ compilers support a predefined macro "__FUNC__" which evaluates to the name of the current function. For member functions it evaluates to "ClassName::MemberFunctionName".
If your compiiler doesn't support this, the Borland C/C++ compiler (v5.something) does and it can be downloaded from their site (free, but you have to register).
regards,
-Brent