i_uintptr_t

joel-q發表於2024-06-21
#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;
}