Shriramana, On 4/4/07, Shriramana Sharma <jamadagni@xxxxxxxxx> wrote:
Hello. Is providing an input to a function by constant reference more efficient than passing it by value? In what way. For ex: int addOne ( const & int inValue ) { return inValue + 1 ; } vs: int addOne ( int inValue ) { return inValue + 1 ; } or: void printThis ( const & int inValue ) { cout << inValue ; } vs: void printThis ( int inValue ) { cout << inValue ; } I think passing as const & would be more efficient since passing by value would involve copying the value whereas passing by const & would skip this step. Am I right? Or is there something else?
I have seen many programs making use const reference parameters in order to inform the compiler that the parameter is read-only, and hence should be better optimized. Unfortunately, this intent is at odds with the C++ language definition. The const keyword says that the storage may not be modified through the given name. What it does not say is that the storage cannot be modified through some other name. With the exception of variables directly declared const, which means you can only initialize them, const is basically ineffective a improving run-time performance. It does, however, catch errors in the programming process. \Steve -- Steve Grägert <steve@xxxxxxxxxxxx> Jabber xmpp://graegerts@xxxxxxxxxx Internet http://eth0.graegert.com, http://blog.graegert.com - To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html