c++pointersstl

Convert "this" pointer to string


In a system where registered objects must have unique names, I want to use/include the object's this pointer in the name. I want the simplest way to create ??? where:

std::string name = ???(this);


Solution

  • You could use string representation of the address:

    #include <sstream> //for std::stringstream 
    #include <string>  //for std::string
    
    const void * address = static_cast<const void*>(this);
    std::stringstream ss;
    ss << address;  
    std::string name = ss.str();