Skip to content

Commit e0f940b

Browse files
author
Colin Robertson
committed
Adapt and adopt PR 988 solution
1 parent 2dfb21b commit e0f940b

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

docs/cpp/codesnippet/CPP/how-to-create-and-use-shared-ptr-instances_3.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
vector<shared_ptr<Song>> v {
22
make_shared<Song>(L"Bob Dylan", L"The Times They Are A Changing"),
33
make_shared<Song>(L"Aretha Franklin", L"Bridge Over Troubled Water"),
4-
make_shared<Song>(L"Thal�a", L"Entre El Mar y Una Estrella")
4+
make_shared<Song>(L"Thalía", L"Entre El Mar y Una Estrella")
55
};
66

77
vector<shared_ptr<Song>> v2;

docs/cpp/codesnippet/CPP/how-to-create-and-use-shared-ptr-instances_4.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
vector<shared_ptr<MediaAsset>> assets {
22
make_shared<Song>(L"Himesh Reshammiya", L"Tera Surroor"),
3-
make_shared<Song>(L"Penaz Masani", L"Tu Dil De De")),
3+
make_shared<Song>(L"Penaz Masani", L"Tu Dil De De"),
44
make_shared<Photo>(L"2011-04-06", L"Redmond, WA", L"Soccer field at Microsoft.")
55
};
66

@@ -18,5 +18,5 @@
1818
{
1919
// We know that the photos vector contains only
2020
// shared_ptr<Photo> objects, so use static_cast.
21-
wcout << "Photo location: " << (static_pointer_cast<Photo>(p))->location_ << endl;
21+
wcout << "Photo location: " << (static_pointer_cast<Photo>(p))->location << endl;
2222
}

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,57 @@ The following illustration shows several `shared_ptr` instances that point to on
1313

1414
![Shared pointer diagram](../cpp/media/shared_ptr.png "Shared pointer diagram")
1515

16+
## Example setup
17+
18+
The examples that follow all assume that you've included the required headers and declared the required types, as shown here:
19+
20+
```cpp
21+
// shared_ptr-examples.cpp
22+
// The following examples assume these declarations:
23+
#include <algorithm>
24+
#include <iostream>
25+
#include <memory>
26+
#include <string>
27+
#include <vector>
28+
29+
struct MediaAsset
30+
{
31+
virtual ~MediaAsset() = default; // make it polymorphic
32+
};
33+
34+
struct Song : public MediaAsset
35+
{
36+
std::wstring artist;
37+
std::wstring title;
38+
Song(const std::wstring& artist_, const std::wstring& title_) :
39+
artist{ artist_ }, title{ title_ } {}
40+
};
41+
42+
struct Photo : public MediaAsset
43+
{
44+
std::wstring date;
45+
std::wstring location;
46+
std::wstring subject;
47+
Photo(
48+
const std::wstring& date_,
49+
const std::wstring& location_,
50+
const std::wstring& subject_) :
51+
date{ date_ }, location{ location_ }, subject{ subject_ } {}
52+
};
53+
54+
using namespace std;
55+
56+
int main()
57+
{
58+
// The examples go here, in order:
59+
// Example 1
60+
// Example 2
61+
// Example 3
62+
// Example 4
63+
// Example 6
64+
}
65+
```
66+
1667
## Example 1
1768
1869
Whenever possible, use the [make_shared](../standard-library/memory-functions.md#make_shared) function to create a `shared_ptr` when the memory resource is created for the first time. `make_shared` is exception-safe. It uses the same call to allocate the memory for the control block and the resource, and thereby reduces the construction overhead. If you do not use `make_shared`, then you have to use an explicit new expression to create the object before you pass it to the `shared_ptr` constructor. The following example shows various ways to declare and initialize a `shared_ptr` together with a new object.

0 commit comments

Comments
 (0)