Skip to content

Instantly share code, notes, and snippets.

@xunkai55
Created January 18, 2016 11:46
Show Gist options
  • Save xunkai55/9fddf7e7454e3ad76dfe to your computer and use it in GitHub Desktop.
Save xunkai55/9fddf7e7454e3ad76dfe to your computer and use it in GitHub Desktop.
Use raw pointers to access an object owned by a unique_ptr
#include <memory>
#include <iostream>
struct A {
int x;
};
typedef std::unique_ptr<A> APtr;
int main() {
A *a_ptr;
std::cout << (int)a_ptr << std::endl;
{
APtr a(new A);
a_ptr = a.get();
std::cout << (int)a_ptr << std::endl;
a->x = 5;
std::cout << a->x << " " << a_ptr->x << std::endl;
a_ptr->x = 10;
std::cout << a->x << " " << a_ptr->x << std::endl;
}
std::cout << (int)a_ptr << " " << (a_ptr == nullptr) << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment