English 中文(简体)
改变“这”点,以示
原标题:Convert "this" pointer to string

在登记物体必须具有独特名称的系统中,我想使用/列入标的<代码>。 我想创造<代码>的最简单方式?

std:string name = ???

最佳回答

您可以利用地址的严格表述:

#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(); 
问题回答

你们指的是把点本身作为扼杀手段的形式?

std::ostringstream address;
address << (void const *)this;
std:string name = address.str();

或......在我照此作时,所有其他类似答案都是一样的!

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

你们可以利用这个要点的地址,把这种描述作为描述的价值?

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;

长期以来,你可以以该系统提供的种类取代未签字。 视窗:uintptr_t

在一个登记物体必须具有独特名称的系统中,我想以这个名字使用/列入物体。

物体地址不一定独特。 例: 您积极分配这一物体,同时使用该物体,删除该物体,然后再分配另一个此类物体。 新分配的物体可能与前一个物体相同。

找到一个独一无二的名称是更好的办法。 例如:

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




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?