Skip to content

Commit 748eeed

Browse files
committed
shared_ptr example code uses concrete types
1 parent 66988cc commit 748eeed

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

books/RayTracingInOneWeekend.html

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -818,8 +818,7 @@
818818
if (dot(ray_direction, outward_normal) > 0.0) {
819819
// ray is inside the sphere
820820
...
821-
}
822-
else {
821+
} else {
823822
// ray is outside the sphere
824823
...
825824
}
@@ -920,7 +919,7 @@
920919
return false;
921920
}
922921
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
923-
[Listing [sphere-final]: <kbd>[sphere.h]</kbd> The sphere class with normal determination]
922+
[Listing [sphere-final]: <kbd>[sphere.h]</kbd> The `sphere` class with normal determination]
924923

925924
</div>
926925

@@ -987,15 +986,25 @@
987986
Typically, a shared pointer is first initialized with a newly-allocated object, something like this:
988987

989988
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
990-
shared_ptr<thing> thing_ptr = make_shared<thing>(thing_constructor_params ...);
991-
auto thing2_ptr = make_shared<thing2>(thing2_constructor_params ...);
989+
shared_ptr<double> double_ptr = make_shared<double>(0.37);
990+
shared_ptr<vec3> vec3_ptr = make_shared<vec3>(1.414214, 2.718281, 1.618034);
991+
shared_ptr<sphere> sphere_ptr = make_shared<sphere>(vec3(0,0,0), 1.0);
992992
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
993993
[Listing [shared-ptr]: An example allocation using `shared_ptr`]
994994

995995
`make_shared<thing>(thing_constructor_params ...)` allocates a new instance of type `thing`, using
996-
the constructor parameters. It returns a `shared_ptr<thing>`. The second line shows a simpler form
997-
using the `auto` type declaration (the type can be automatically deduced by the return type of
998-
`make_shared<thing2>(...)`.
996+
the constructor parameters. It returns a `shared_ptr<thing>`.
997+
998+
Since the type can be automatically deduced by the return type of `make_shared<type>(...)`, the
999+
above lines can be more simply expressed using C++'s `auto` type specifier:
1000+
1001+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1002+
auto double_ptr = make_shared<double>(0.37);
1003+
auto vec3_ptr = make_shared<vec3>(1.414214, 2.718281, 1.618034);
1004+
auto sphere_ptr = make_shared<sphere>(vec3(0,0,0), 1.0);
1005+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1006+
[Listing [shared-ptr-auto]: An example allocation using `shared_ptr` with `auto` type]
1007+
9991008
</div>
10001009

10011010
We'll use shared pointers in our code, because it allows multiple objects to use a common object

0 commit comments

Comments
 (0)