内置指针类型 i_uintptr_t
在内置类型中定义operator i_uintptr_t()
可以实现对内置类型与内置指针类型之间的转换,即做取内置类型地址的操作。
#include <iostream>
struct i_uintptr_t {
i_uintptr_t(): ptr{} {}
i_uintptr_t(uintptr_t ptr): ptr{ptr} {}
~i_uintptr_t() { ptr = 0; }
operator uintptr_t() { return ptr; }
operator void*() { std::cout<<"just"<<std::endl; return (void*)ptr; }
uintptr_t ptr;
};
int main()
{
int i = 42;
i_uintptr_t i_uintptr{reinterpret_cast<uintptr_t>(&i)};
int *pi = reinterpret_cast<int *>((void *)i_uintptr);
std::cout << *pi;
return 0;
}