C++ style guide: Difference between revisions

Jump to navigation Jump to search
294 bytes added ,  24 August 2021
m
Better example for const reference usage
m (Better example for const reference usage)
Line 219: Line 219:
|-
|-
| <syntaxhighlight lang="c++">
| <syntaxhighlight lang="c++">
void foo (const int& a_ref)
void foo (const std::string& str_ref)
{
{
   // foo does not change content of `a_ref`
   // foo does not change content of `str_ref`
}
}


void bar ()
void bar ()
{
{
   int a = 42;
   std::string str ("This is a large variable, however as a reference it will take up just 8 bytes on the stack when passed to the subroutine foo()");
   foo (a);
   foo (str);
}
}
</syntaxhighlight>
</syntaxhighlight>
| <syntaxhighlight lang="c++">
| <syntaxhighlight lang="c++">
void foo (int& a_ref)
void foo (std::string str_copy)
{
{
   // foo does not change content of `a_ref`
   // foo does not change content of `str_copy`
}
}


void bar ()
void bar ()
{
{
   int a = 42;
   std::string str ("This is a large variable that will be copied on to the stack and passed as a temporary variable to the subroutine foo()");
   foo (a);
   foo (str);
}
}
</syntaxhighlight>
</syntaxhighlight>
1,072

edits

Navigation menu