1. boost::scoped_ptr is a smart pointer that is the sole owner of a dynamically allocated object and cannot be copied or moved. #include <boost/scoped_ptr.hpp> #include <iostream> int main() { boost::scoped_ptr<)); std::cout << *p <…
unique_ptr最先在boost中被定义,后来被C++标准委员会选中为C++11的feature之一. std::unique_ptr is a smart pointer that retains sole ownership of an object through a pointer and destroys that object when the unique_ptr goes out of scope. No two unique_ptr instances can manage…
Smart pointer line 58之后smart pointer里的计数已经是0,所以会真正释放它引用的对象,调用被引用对象的析构函数.如果继续用指针访问,会出现如下图的内存访问异常.所以说如果选择了用智能指针,就不要再试图用其他方式再去访问对象了. // sharedTest.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <boost/…
13.8 Write a smart pointer class. A smart pointer is a data type, usually implemented with templates, that simulates a pointer while also providing automatic garbage collection. It automatically counts the number of references to a SmartPointer<T*>…
Here are two simple questions. Problem A #include <string> include <iostream> using namespace std; class vehicle { public: vehicle(const string& name); virtual ~vehicle(){} void PrintOwnerInfo(); private: string driver_name_; }; vehicle::v…