Skip to content

Commit e66b591

Browse files
authored
Merge pull request #4931 from MicrosoftDocs/main
5/31 AM Publish
2 parents 9e7e8c6 + dac7f43 commit e66b591

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
void use_shared_ptr_by_value(shared_ptr<int> sp);
2+
3+
void use_shared_ptr_by_reference(shared_ptr<int>& sp);
4+
void use_shared_ptr_by_const_reference(const shared_ptr<int>& sp);
5+
6+
void use_raw_pointer(int* p);
7+
void use_reference(int& r);
8+
9+
void test() {
10+
auto sp = make_shared<int>(5);
11+
12+
// Pass the shared_ptr by value.
13+
// This invokes the copy constructor, increments the reference count, and makes the callee an owner.
14+
use_shared_ptr_by_value(sp);
15+
16+
// Pass the shared_ptr by reference or const reference.
17+
// In this case, the reference count isn't incremented.
18+
use_shared_ptr_by_reference(sp);
19+
use_shared_ptr_by_const_reference(sp);
20+
21+
// Pass the underlying pointer or a reference to the underlying object.
22+
use_raw_pointer(sp.get());
23+
use_reference(*sp);
24+
25+
// Pass the shared_ptr by value.
26+
// This invokes the move constructor, which doesn't increment the reference count
27+
// but in fact transfers ownership to the callee.
28+
use_shared_ptr_by_value(move(sp));
29+
}

docs/cpp/how-to-create-and-use-shared-ptr-instances.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
description: "Learn more about: How to: Create and Use shared_ptr instances"
33
title: "How to: Create and use shared_ptr instances"
44
ms.custom: "how-to"
5-
ms.date: "11/19/2019"
5+
ms.date: "05/26/2023"
66
ms.topic: "conceptual"
77
ms.assetid: 7d6ebb73-fa0d-4b0b-a528-bf05de96518e
88
---
@@ -81,13 +81,13 @@ The following example shows how to declare and initialize `shared_ptr` instances
8181
8282
`shared_ptr` is also helpful in C++ Standard Library containers when you're using algorithms that copy elements. You can wrap elements in a `shared_ptr`, and then copy it into other containers with the understanding that the underlying memory is valid as long as you need it, and no longer. The following example shows how to use the `remove_copy_if` algorithm on `shared_ptr` instances in a vector.
8383
84-
[!code-cpp[stl_smart_pointers#4](codesnippet/CPP/how-to-create-and-use-shared-ptr-instances_3.cpp)]
84+
[!code-cpp[stl_smart_pointers#3](codesnippet/CPP/how-to-create-and-use-shared-ptr-instances_3.cpp)]
8585
8686
## Example 4
8787
8888
You can use `dynamic_pointer_cast`, `static_pointer_cast`, and `const_pointer_cast` to cast a `shared_ptr`. These functions resemble the **`dynamic_cast`**, **`static_cast`**, and **`const_cast`** operators. The following example shows how to test the derived type of each element in a vector of `shared_ptr` of base classes, and then copy the elements and display information about them.
8989
90-
[!code-cpp[stl_smart_pointers#5](codesnippet/CPP/how-to-create-and-use-shared-ptr-instances_4.cpp)]
90+
[!code-cpp[stl_smart_pointers#4](codesnippet/CPP/how-to-create-and-use-shared-ptr-instances_4.cpp)]
9191
9292
## Example 5
9393
@@ -105,11 +105,13 @@ You can pass a `shared_ptr` to another function in the following ways:
105105
106106
- Sometimes, for example in a `std::vector<shared_ptr<T>>`, you may have to pass each `shared_ptr` to a lambda expression body or named function object. If the lambda or function doesn't store the pointer, then pass the `shared_ptr` by reference to avoid invoking the copy constructor for each element.
107107
108+
[!code-cpp[stl_smart_pointers#5](codesnippet/CPP/how-to-create-and-use-shared-ptr-instances_5.cpp)]
109+
108110
## Example 6
109111
110112
The following example shows how `shared_ptr` overloads various comparison operators to enable pointer comparisons on the memory that is owned by the `shared_ptr` instances.
111113
112-
[!code-cpp[stl_smart_pointers#3](codesnippet/CPP/how-to-create-and-use-shared-ptr-instances_6.cpp)]
114+
[!code-cpp[stl_smart_pointers#6](codesnippet/CPP/how-to-create-and-use-shared-ptr-instances_6.cpp)]
113115
114116
## See also
115117

docs/cpp/references-cpp.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ struct S {
6969

7070
int main() {
7171
S s; // Declare the object.
72-
S& SRef = s; // Declare the reference.
72+
S& SRef = s; // Declare and initialize the reference.
7373
s.i = 3;
7474

7575
printf_s("%d\n", s.i);

0 commit comments

Comments
 (0)