C ++ 11:智能指针使用 [英] C++ 11: smart pointers usage

查看:286
本文介绍了C ++ 11:智能指针使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用智能的最佳做法是什么?在某些情况下,我更喜欢使用原始指针而不是智能指针?

What are the best practices for using smart. Are there situations in which i should prefer using raw pointer instead of smart pointers?

例如,如果我知道类A创建类B并且是B - 如果有理由使用智能指针?

For example, if i know that class A creates class B and is the only owner of B - if there a reason to use smart pointers?

如果你知道有关该主题的任何好的文章,请分享。

If you know of any good articles on that subject, please share.

推荐答案

如果指针拥有对象,则使用智能指针。

If the pointer owns the object at any time, then use a smart pointer.

如果指针拥有该对象(即该对象由另一个智能指针拥有并且保证超出该指针),请使用原始指针。

If the pointer does not own the object (i.e. the object is owned by another smart pointer and guaranteed to outlive this pointer), use a raw pointer.

这篇关于C ++ 11:智能指针使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆