Posts

Showing posts with the label functions

C++: To Reference or Not

Constant Something Reference or Something constant reference... I ask myself this in a similar manner as Prince Hamlet at the start of the nunnery scene... For as a C++ programmer and English speaker I've always found myself most comfortable using the phraseology thus: const int X (0); And to pass this to a function: void foo (const int& p_X); I find this most useful, we're passing a constant integer reference to the function... However, I was recently challenged that this meant something else to a reader, her input was that it meant "constant integer" reference, that is we would ONLY be able to pass "const int" instances, not "int" instances.   The thinking being: const int X(0); void foo(const int& p_X); foo(X); Would compile, whilst: int Y(42) void bar (const int& p_Y); bar (Y); Would fail to compile or at least spit out a warning because "int" was not able to be passed into the function as "constant integer" and ...