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);
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();
ss << this
might invoke operator<<
which accepts T const * const
as argument, in which case you'll not get the address as string. Here is what I mean : coliru.stacked-crooked.com/a/cded799e93012de6 –
Kincaid std::string address = std::to_string((unsigned long long)(void**)this);
, which also works if you need the address as an integer, not a string: unsigned long long address = (unsigned long long)(void**)this;
–
Prop 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!
A simpler one-liner that doesn't require a whole stringstream:
std::string address = std::to_string((unsigned long long)(void**)this);
Also implied, works with pointers of any time, not just this. Also works if you just need it as an integer:
unsigned long long address = (unsigned long long)(void**)this;
You can replace unsigned long long with a type alias that is provided by the system. On Windows it's uintptr_t
.
#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;
}
You could use ostringstream the this pointer's address and put that ostringstream's value as string?
In a system where registered objects must have unique names, I want to use/include the object's this pointer in the name.
An object's address is not necessarily unique. Example: You dynamically allocate such an object, use it for a while, delete it, and then allocate another such object. That newly allocated object might well have the same the object address as the previous.
There are far better ways to generate a unique name for something. A gensym counter, for example:
// Base class for objects with a unique, autogenerated name.
class Named {
public:
Named() : unique_id(gensym()) {}
Named(const std::string & prefix) : unique_id(gensym(prefix)) {}
const std::string & get_unique_id () { return unique_id; }
private:
static std::string gensym (const std::string & prefix = "gensym");
const std::string unique_id;
};
inline std::string Named::gensym (const std::string & prefix) {
static std::map<std::string, int> counter_map;
int & entry = counter_map[prefix];
std::stringstream sstream;
sstream << prefix << std::setfill('0') << std::setw(7) << ++entry;
return sstream.str();
}
// Derived classes can have their own prefix. For example,
class DerivedNamed : public Named {
public:
DerivedNamed() : Named("Derived") {}
};
A simple and explicit way to convert a pointer into a string:
std::string str = std::to_string(reinterpret_cast<uintptr_t>(ptr));
© 2022 - 2024 — McMap. All rights reserved.
delete
thennew
in one instance and you could get the same address. – Kilohertz