如何通过共享指针获取Object?

时间:2023-02-13 10:28:06

I have a query. Can we get the object that a shared pointer points to directly? Or should we get the underlying RAW pointer through get() call and then access the corresponding object?

我有一个查询。我们可以直接获得共享指针指向的对象吗?或者我们应该通过get()调用获取底层RAW指针然后访问相应的对象?

2 个解决方案

#1


41  

You have two options to retrieve a reference to the object pointed to by a shared_ptr. Suppose you have a shared_ptr variable named ptr. You can get the reference either by using *ptr or *ptr.get(). These two should be equivalent, but the first would be preferred.

您有两个选项可以检索对shared_ptr指向的对象的引用。假设您有一个名为ptr的shared_ptr变量。您可以使用* ptr或* ptr.get()获取引用。这两个应该是等价的,但第一个是首选。

The reason for this is that you're really attempting to mimic the dereference operation of a raw pointer. The expression *ptr reads "Get me the data pointed to by ptr", whereas the expression *ptr.get() "Get me the data pointed to by the raw pointer which is wrapped inside ptr". Clearly, the first describes your intention much more clearly.

原因是你真的试图模仿原始指针的取消引用操作。表达式* ptr读取“让我获取ptr指向的数据”,而表达式* ptr.get()“获取包含在ptr中的原始指针指向的数据”。显然,第一个更清楚地描述了你的意图。

Another reason is that shared_ptr::get() is intended to be used in a scenario where you actually need access to the raw pointer. In your case, you don't need it, so don't ask for it. Just skip the whole raw pointer thing and continue living in your safer shared_ptr world.

另一个原因是shared_ptr :: get()旨在用于您实际需要访问原始指针的场景中。在你的情况下,你不需要它,所以不要求它。只需跳过整个原始指针,继续生活在更安全的shared_ptr世界中。

#2


14  

Ken's answer above (or below, depending on how these are sorted) is great. I don't have enough reputation to comment, otherwise I would.

Ken的答案在上面(或下面,取决于这些是如何排序的)是很好的。我没有足够的声誉来评论,否则我会。

I'd just like to add that you can also use the -> operator directly on a shared_ptr to access members of the object it points to.

我只想补充一点,您也可以直接在shared_ptr上使用 - >运算符来访问它指向的对象的成员。

The boost documentation gives a great overview.

boost文档提供了一个很好的概述。

EDIT

Now that C++11 is widely adopted, std::shared_ptr should be preferred to the Boost version. See the dereferencing operators here.

现在C ++ 11被广泛采用,std :: shared_ptr应该优先于Boost版本。请在此处查看解除引用运算符。

#1


41  

You have two options to retrieve a reference to the object pointed to by a shared_ptr. Suppose you have a shared_ptr variable named ptr. You can get the reference either by using *ptr or *ptr.get(). These two should be equivalent, but the first would be preferred.

您有两个选项可以检索对shared_ptr指向的对象的引用。假设您有一个名为ptr的shared_ptr变量。您可以使用* ptr或* ptr.get()获取引用。这两个应该是等价的,但第一个是首选。

The reason for this is that you're really attempting to mimic the dereference operation of a raw pointer. The expression *ptr reads "Get me the data pointed to by ptr", whereas the expression *ptr.get() "Get me the data pointed to by the raw pointer which is wrapped inside ptr". Clearly, the first describes your intention much more clearly.

原因是你真的试图模仿原始指针的取消引用操作。表达式* ptr读取“让我获取ptr指向的数据”,而表达式* ptr.get()“获取包含在ptr中的原始指针指向的数据”。显然,第一个更清楚地描述了你的意图。

Another reason is that shared_ptr::get() is intended to be used in a scenario where you actually need access to the raw pointer. In your case, you don't need it, so don't ask for it. Just skip the whole raw pointer thing and continue living in your safer shared_ptr world.

另一个原因是shared_ptr :: get()旨在用于您实际需要访问原始指针的场景中。在你的情况下,你不需要它,所以不要求它。只需跳过整个原始指针,继续生活在更安全的shared_ptr世界中。

#2


14  

Ken's answer above (or below, depending on how these are sorted) is great. I don't have enough reputation to comment, otherwise I would.

Ken的答案在上面(或下面,取决于这些是如何排序的)是很好的。我没有足够的声誉来评论,否则我会。

I'd just like to add that you can also use the -> operator directly on a shared_ptr to access members of the object it points to.

我只想补充一点,您也可以直接在shared_ptr上使用 - >运算符来访问它指向的对象的成员。

The boost documentation gives a great overview.

boost文档提供了一个很好的概述。

EDIT

Now that C++11 is widely adopted, std::shared_ptr should be preferred to the Boost version. See the dereferencing operators here.

现在C ++ 11被广泛采用,std :: shared_ptr应该优先于Boost版本。请在此处查看解除引用运算符。