File tree Expand file tree Collapse file tree 1 file changed +6
-4
lines changed Expand file tree Collapse file tree 1 file changed +6
-4
lines changed Original file line number Diff line number Diff line change @@ -31,13 +31,15 @@ This check covers non-obvious and easy-to-miss behavior when assigning a referen
31
31
This sample shows a variable definition that makes a potentially expensive copy when assigned a reference:
32
32
33
33
``` cpp
34
- MyClass& ref = ...;
35
- auto var = ref; // C26820 (`var` takes a copy of the object referred to by `ref`)
34
+ const Object& MyClass::getRef () { ... }
35
+ ...
36
+ auto ref = myclass.getRef(); // C26820 (` ref ` takes a copy of the returned object)
36
37
```
37
38
38
39
To resolve this issue, declare the variable by using `const auto&` instead:
39
40
40
41
```cpp
41
- MyClass& ref = ...;
42
- const auto & var = ref; // OK
42
+ const Object& MyClass::getRef() { ... }
43
+ ...
44
+ const auto& ref = myclass.getRef(); // OK
43
45
```
You can’t perform that action at this time.
0 commit comments