//To compile and run: c++ -std=c++11 unique_ptr.cc && ./a.out //Small program showing ownership w/ unique_ptr. //Class G4Class_cxx11 owns a pointer to an instance of G4Something via //std::unique_ptr. //The main function exercises owenership getting/setting pointer via old //interface using raw-pointers and unique_ptr, pay attention to the address of the //owned G4Something object. Note how the pointer becomes nullptr when ownership is //moved around and only one pointer referencing the pointee is existing at any given //time #include #include #include struct G4Something { G4Something(double _a,double _b,double _c): a(_a),b(_b),c(_c) {} G4Something() = default; G4Something(const G4Something& other ) : a(other.a),b(other.b),c(other.c) { std::cout<<"G4Something copy constructor called"< pointer; G4Class_cxx11() : pointer() { std::cout<<"G4Class_cxx11::Default constructor"<(new G4Something(*other.pointer) ) ); } std::cout<<"G4Class_cxx11::Copy constructor"<( new G4Something(*other.pointer) ) ); } else { pointer = std::move( std::unique_ptr() ); } std::cout<<"G4Class_cxx11::Assignment operator "<&& p ) { //**************************** std::cout<<"G4Class_cxx11::set w/ unique_ptr called"< get() { //**************************** std::cout<<"G4Class_cxx11::get unique_ptr called "<(new G4Something(*p) ); } //Ambigous, to remove G4Something* get_old() const { std::cout<<"G4Class_cxx11::get pointer old style called"<set( smt ); std::cout<<"Set done: "<<*cnew<get_old(); const G4Something& temp2 = cnew->get_const(); std::cout<<"Old get pointer returns: "< smt2(new G4Something{1,2,3} ); std::cout<<*smt2< pp = cnew2.get(); std::cout<<"get ownership: "<<*pp<