Posts

Showing posts with the label passing

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 ...

Software Development : Get Constant in C++

Image
What does " const " mean to you?  Does it just mean a value can not be changed?  If so, you may want to read on! Const is one of those things, which though not unique to C++ is given more meaning when you leverage C++ fully, const allows you to not only define a value as invariant, but also to instruct other programmers coming to your code how a value should be used, how when it is passed as a parameter it should be treated and ultimately how to protect data from needless alteration, de-synchronisation or simple corruption. Const is therefore your friend, and if you've come to C++ from C, Java, C#, Python or one of the other myriad of languages which don't treat const with as much relevance as C++ you may want to read more than I can say on the topic.  Bjarne Stroustrop (the inventor of C++) and other authors on the topic (notably Scott Meyers & Herb Sutter) explain in much more detail than I ever could, but for brevity here are two examples of const from my own c...