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);
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();
You mean format the pointer itself as a string?
std::ostringstream address;
address << (void const *)this;
std:string name = address.str();
Or ... yes, all the other equivalent answers in the time it took me to type this!
#include <sstream>
#include <iostream>
struct T
{
T()
{
std::ostringstream oss;
oss << (void*)this;
std::string s(oss.str());
std::cout << s << std::endl;
}
};
int main()
{
T t;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With