My goal is to create unique_ptr<void*> where void* can point to any object type. This gives me a way to use unique_ptr to point to state object that shares the same signature. But below code doesn't seem to match my expectation where p.get() should be equal to v and s1 address. I suspect it has to do the line that try to wrap a unqiue_ptr on top of s1 to own it. But address has changed.
struct State1
{
public:
std::string name;
std::string address;
};
int main() {
auto s1 = new State1;
s1->name = "name";
std::cout<<"s1 address: "<<s1<<"\n";
auto v = (void*)s1;
std::cout<<"v address: "<<v<<"\n";
auto p = std::make_unique<void*>(s1);
std::cout<<"p.get address: "<<p.get()<<"\n";
auto r = static_cast<State1*>((void*)p.get());
std::cout<<"r address: "<<r<<"\n";
}
s1 address: 0x55dcf389aeb0
v address: 0x55dcf389aeb0
p.get address: 0x55dcf389b310
r address: 0x55dcf389b310
Aucun commentaire:
Enregistrer un commentaire