什么是智能指针及何时使用IS2120@CSDN.BG57IV3

news/2024/7/4 9:29:13

//z 2013-03-27 00:02:51.T2817905664.K[T2,L47,R1,V27]

IS2120@CSDN.BG57IV3
智能指针是一个包装了裸指针的类,用于管理其所指向对象的生命期。
A smart pointer is a class that wraps a "bare" C++ pointer, to manage the lifetime of the 
object being pointed to.

使用裸指针,当对象不再使用时,程序员需要明确地将之销毁掉。
With "bare" C++ pointers, the programmer has to explicitly destroy the object when it is no 
longer useful.

// Need to create the object to achieve some goal
MyObject* ptr = new MyObject(); 
ptr->DoSomething();// Use the object in some way.
delete ptr; // Destroy the object. Done with it.
// Wait, what if DoSomething() raises an exception....

相比之下,智能指针定义了一套何时销毁对象的策略。
通过使用智能指针,你仍旧不得不创建一个对象,但是你不在担心如何销毁它。
A smart pointer by comparison defines a policy as to when the object is destroyed. You still 
have to create the object, but you no longer have to worry about destroying it.

SomeSmartPtr<MyObject> ptr(new MyObject());
ptr->DoSomething(); // Use the object in some way.

// Destruction of the object happens, depending 
// on the policy the smart pointer class uses.

// Destruction would happen even if DoSomething() 
// raises an exception

在用的最简单的一项策略是伴随着所包装对象的生命周期。
The simplest policy in use involves the scope of the smart pointer wrapper object, such as 
implemented by boost::scoped_ptr or std::tr1::scoped_ptr.

void f()
{
    {
       boost::scoped_ptr<MyObject> ptr(new MyObject());
       ptr->DoSomethingUseful();
    } // boost::scopted_ptr goes out of scope -- 
      // the MyObject is automatically destroyed.

    // ptr->Oops(); // Compile error: "ptr" not defined
                    // since it is no longer in scope.
}


Note that scoped_ptr instances cannot be copied. This prevents the pointer from being deleted 
multiple times (incorrectly). You can however pass references to it around to other functions 
you call.

Scoped pointers are useful when you want to tie the lifetime of the object to a particular 
block of code, or if you embedded it as member data inside another object, the lifetime of 
that other object. The object exists until the containing block of code is exitted, or until 
the containing object is itself destroyed.

A more complex smart pointer policy involves reference counting the pointer. This does allow 
the pointer to be copied. When the last "reference" to the object is destroyed, the object is
 deleted. This policy is implemented by boost::shared_ptr and std::tr1:shared_ptr.

void f()
{
    typedef std::tr1::shared_ptr<MyObject> MyObjectPtr; // Nice short alias.
    MyObjectPtr p1; // Empty
    {
        MyObjectPtr p2(new MyObject());
        // There is now one "reference" to the created object
        p1=p2; // Copy the pointer.
        // There is are now two references to the object.
    } // p2 is destroyed, leaving one reference to the object.
} // p1 is destroyed, leaving a reference count of zero. 
  // The object is deleted.
Reference counted pointers are very useful when the lifetime of your object is much more 
complicated, and is not tied directly to a particular section of code or to another object.

There is one drawback to reference counted pointers  the possibility of creating a dangling 
reference.///

// Create the smart pointer on the heap
MyObjectPtr* pp = new MyObjectPtr(new MyObject())
// Hmm, we forgot to destroy the smart pointer,
// because of that, the object is never destroyed!
Another possibility is creating circular references.

struct Owner {
   boost::shared_ptr<Owner> other;
};

boost::shared_ptr<Owner> p1 (new Owner());
boost::shared_ptr<Owner> p2 (new Owner());
p1->other = p2; // p1 references p2
p2->other = p1; // p2 references p1

// Oops, the reference count of of p1 and p2 never goes to zero!
// The objects are never destroyed!
To work around this problem, both boost and std::tr1 define weak_ptr to define a weak (
uncounted) reference to a shared_ptr.

Also note that the existing standard C++ library does define a special kind of smart pointer 
std::auto_ptr. It is very much like a scoped pointer, except that it also has the "special" 
dangerous ability to be copied  which also unexpectedly transfers ownership!

std::auto_ptr<MyObject> p1 (new Owner());
std::auto_ptr<MyObject> p2 = p1; // Copy and transfer ownership. 
                                 // p1 gets set to empty!
p2->DoSomething(); // Works.
p1->DoSomething(); // Oh oh. Hopefully raises some NULL pointer exception.IS2120@CSDN.BG57IV3
 

转载于:https://www.cnblogs.com/IS2120/archive/2013/03/26/6745759.html


http://www.niftyadmin.cn/n/3244446.html

相关文章

Lina 创业

http://www.linamao.com/ Home Lina的英文教程 与lina合作 与lina联系 交换链接 关于Lina 关于RoseforLove.com 网络创业, 网下创业, 在美国创业! –欢迎与 Lina 一起学习&#xff0c;分享&#xff0c; 鼓励&#xff0c; 成长! 中国产品和品牌如何开拓美国市场? Posted by Li…

另类生意闷声挣钱:一人经营几十万个网站(图)

另类生意闷声挣钱&#xff1a;一人经营几十万个网站(图) 新华网<iframe align"left" marginwidth"0" marginheight"0" src"http://news.wenxuecity.com/adinfo/newsview.php" frameborder"0" width"300" scro…

PieLove 之 数据分析帝.(ZZ)(is2120)

//z 2015-11-18 13:11:20 L.43 38920 BG57IV3XCL T3124966025.K.F2308917803[T65,L1051,R44,V2186] 高息揽储->正常还本付息->老乡们奔走相告->更高的息揽储->老乡们几倍于前面的资金跟进…(循环一段时间&#xff0c;有长有短)…->突然告知不能还本付息->老乡…

手指沾油彩画布上乱摸 6岁女孩画作赚20万英镑

据英国媒体21日报道&#xff0c;美国纽约州6岁小女孩玛拉・奥姆斯泰德靠出售她的艺术画&#xff0c;目前已经赚来了20万英镑的收入。www.6park.com 玛拉・奥姆斯泰德是美国纽约州宾厄姆顿市人&#xff0c;玛拉的父亲马克喜欢绘画&#xff0c;4年前&#xff0c;马克操起画笔画了…

Memo

bingo 转载于:https://www.cnblogs.com/IS2120/archive/2013/04/14/6745757.html

good art home page.

Contemporary Art Abstract Paintings ofFrank Ignizio http://www.frankignizio.com/index.html

wix 安装包调试(zz.IS2120@BG57IV3.csdn)

wix 安装包调试 //z 2013-04-24 14:01:39 IS2120BG57IV3.T4214783824.K[T460,L6754,R218,V7342]安装包日志调试 msiexec /i MyApplication.msi /l*v MyLogFile.txt 卸载包日志调试 msiexec /x MyApplication.msi /l*v MyLogFile.txt 启用 Windows Installer 日志记录 若要自己启…