C++ 将“this”指针转换为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7850125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:35:50  来源:igfitidea点击:

Convert "this" pointer to string

c++pointersstl

提问by Mr. Boy

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

在注册对象必须具有唯一名称的系统中,我想在名称中使用/包含对象的this指针。我想要最简单的方法来创建???

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

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

回答by Nawaz

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(); 

回答by Useless

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!

或者......是的,在我输入这个的时间里所有其他等效的答案!

回答by mloskot

#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;
} 

回答by Aditya Kumar Pandey

You could use ostringstream the this pointer's address and put that ostringstream's value as string?

您可以使用 ostringstream this 指针的地址并将该 ostringstream 的值作为字符串?

回答by David Hammen

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

在注册对象必须具有唯一名称的系统中,我想在名称中使用/包含对象的 this 指针。

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:

有更好的方法可以为某物生成唯一的名称。一个 gensym 计数器,例如:

// 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") {}
};