Question

I've started learnig C++ (year ago) mostly because it's univerzal language IMO, and beacause almost everything is possible. but one thing isn't so: for example we are writing some code inside an object(class) and we need to find out it's name somehow:

class Test
{
public: const char* getMyIdentiferName()
          {
                // what now??
          }
};

well the best option is to use 'this' keywod but that wouldn't help cos 'this' cant return name?

Test thatsMyName;
const char* = thtsMyName.getMyIdentiferName(); //return string "thatsMyName" how?

how do we get 'thatsMyName' string in in some generic function or even template??

ANOTHER EXAMPLE:(please answer this too)

how do we get typeid of some class?

class MyType
{
public: type_info getType()
     {
          return typeid(this); //that wont work of course :)
     {
};

this looks funny but if any of you have some idea on how to achive similar task...

thanks alot.

EDIT: OK, everybodey say it's impossible to get the name of an object, I found out how to get the name:

class Test
{
    public: string getObjectName()
    {
        string arg = typeid(*this).name();
        arg.erase(arg.begin(), arg.begin() + 5);
        arg.erase(0,1);
        return arg;
    }
};


int main() 
{
    Test thisIsMyName;
    cout << thisIsMyName.getObjectName() << endl;
    cin.ignore();
    return 0;
}

EDIT: Big thanks to Fiktik answering my second example who found the way oon how to get the type_info of the object!

Was it helpful?

Solution

The first thing you are asking is not possible. At least not directly.

You could create a macro for variable declaration that would register its name somewhere, something like this:

#define CREATE_VARIABLE(type, name) registerVariable<type>(#name); type name

but this is quite cumbersome and cannot be used everywhere. Why would you even want to have this functionality?

The second thing should work with only little adjustments:

class MyType
{
public:
    const type_info& getType()
    {
        return typeid(*this);
    }
};

OTHER TIPS

What would you need the variable name for? What you're trying to do is impossible; inside an classes' methods, the name of the variable used for accessing an object isn't known.

Consider this example:

Test * myTest = new Test();
Test * myTest2 = myTest;
const char* identifier = myTest2->getMyIdentifierName();

What should getMyIdentifierName() return? "myTest"? "myTest2"? Nothing at all, since the object was allocated dynamically, and therefore no variable can really claim to "hold" the object?

If you just want to know the variable name which you are currently using for referring to an object, why not just use a string literal for it and pass it wherever you need it? But that doesn't seem to make sense in any real-world application.

If you need a name (i.e., a unique identifier) for an object at runtime, you'll best give the class a member name and initialize that with whatever you need.

Getting the "identifier name" of an object won't work, since there is no unique identifier for an object (other than it's address, to be gotten with &). An unlimited number of references/pointers can designate the same object; at the same time, the language allows objects to be reached by other means than identifiers (v[0] where v is a vector is an object, but not an identifier). This is the case in all programming languages that I know, and I know a dozen of them.

As for your second question, you can return a reference to an std::type_info.

struct Test
{
    // pretty useless method
    std::type_info const &type() const { return typeid(*this); }
};

int main()
{
    Test a;
    std::cout << a.type().name() << "\n";
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top