|
818 | 818 | if (dot(ray_direction, outward_normal) > 0.0) {
|
819 | 819 | // ray is inside the sphere
|
820 | 820 | ...
|
821 |
| - } |
822 |
| - else { |
| 821 | + } else { |
823 | 822 | // ray is outside the sphere
|
824 | 823 | ...
|
825 | 824 | }
|
|
920 | 919 | return false;
|
921 | 920 | }
|
922 | 921 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
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] |
924 | 923 |
|
925 | 924 | </div>
|
926 | 925 |
|
|
987 | 986 | Typically, a shared pointer is first initialized with a newly-allocated object, something like this:
|
988 | 987 |
|
989 | 988 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
990 |
| - shared_ptr<thing> thing_ptr = make_shared<thing>(1, true); |
991 |
| - auto thing2_ptr = make_shared<thing2>(2, false); |
| 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); |
992 | 992 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
993 | 993 | [Listing [shared-ptr]: An example allocation using `shared_ptr`]
|
994 | 994 |
|
995 |
| -`make_shared<thing>(1, true)` allocates a new instance of type `thing`, using the constructor |
996 |
| -arguments `(1, true)`. It returns a `shared_ptr<thing>`. This can be simplified as in the second |
997 |
| -line with the `auto` type declaration, because the type is sufficiently defined by the return type |
998 |
| -of `make_shared<thing2>`. |
| 995 | +`make_shared<thing>(thing_constructor_params ...)` allocates a new instance of type `thing`, using |
| 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 | + |
999 | 1008 | </div>
|
1000 | 1009 |
|
1001 | 1010 | We'll use shared pointers in our code, because it allows multiple objects to use a common object
|
|
0 commit comments