/
Const References for Inputs and Non const Pointers for Outputs

Const References for Inputs and Non const Pointers for Outputs

This a convention that Andrew V taught us which is a good convention. Unfortunately a lot of our library code predates this convention but it’s what we should follow with new code.

 

// A good example function showing the convention // Inputs are given as const references // Outputs are given using non const pointers void FOObar(const COLstring& Input, COLstring* pOutput1, int* pOutput2) // This is helpful since it makes it clearer when reading code which uses the // function if a variable will be modified by the function // For example COLstring Input = "Life"; COLstring Output1; int Output2; FOObar(Input, &Output1, &Output2); // If FOObar had used a non const reference like this void FOObar(const COLstring& Input, COLstring* pOutput1, int* pOutput2); // Then the calling code is less obvious COLstring Input = "Life"; COLstring Output1; int Output2; FOObar(Input, Output1, Output2);

 

If you are not yet familiar with C++ references read this guide in wikipedia.

Related content

Static keyword - it's multiple meanings in C++
Static keyword - it's multiple meanings in C++
More like this
What is C++ name mangling?
What is C++ name mangling?
More like this
Set up a dummy 'library' and unit test
Set up a dummy 'library' and unit test
More like this
Initializing data in C/C++
Initializing data in C/C++
More like this
Using a hidden global pointer for functions
Using a hidden global pointer for functions
More like this
Named prefixes with C++ and our string class
Named prefixes with C++ and our string class
More like this