Created
January 18, 2016 11:46
-
-
Save xunkai55/9fddf7e7454e3ad76dfe to your computer and use it in GitHub Desktop.
Use raw pointers to access an object owned by a unique_ptr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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