9.7 CONST PARAMETER AND RETURN TYPE FOR C FUNCTIONS


9.7 CONST PARAMETER AND RETURN TYPE FOR C++ FUNCTIONS

A function may be called either for its returned value or for its side effects. When a function is invoked for only its returned value, you want to make sure that the argument objects supplied to the function do not get inadvertently corrupted by the function through some unintended side effect. When an argument object is passed to a function by value, this is not a worry because what the function gets is a copy of the argument object. So in pass-argument-by-value, there is no way for a function to mess up the argument object.

But it is not always possible to use the pass-by-value mode for passing arguments to functions. For large objects, the copying entailed in pass-by-value may significantly degrade the performance of a program with respect to both time and space considerations.[5] So we must resort to the more efficient modes of passing arguments-pass by reference or pass by pointer. But now we must worry if the writer of a function will accidently corrupt the argument object. When a function is being used purely for its return value and when we cannot pass argument objects to such a function by value, we can use const to characterize the parameters corresponding to those argument objects.

Over the years, this use of const has emerged as a sort of programming convention that tells a human reader of the program that the corresponding argument in the function call will not be modified by the called function [54]. So while the following prototype carries no guarantees that the object for which the parameter will serve as a reference will not get modified by the function f,

       User f(User& u) {           // code           return u;       } 

a guarantee to that effect is entailed by the following version of the function

       User f(const User& u) {           // code           return u;       } 

The const object will be copied elsewhere in the memory when the return statement is executed. So it can be returned as a non-const object without problems.

If in the above example you also wanted to suppress the copying involved in constructing the returned object, you'd have to use the form

       const User& f (const User& u) {           // code           return u;       } 

If the function is simply returning a const object without copying it elsewhere in the memory, then the return type must also be const. For example, the following function will not compile

      User& f (const User& u) {return u;} // WRONG 

Because copying is prohibited when the return statement is executed in this example, the const qualifier cannot be stripped away. You need to declare const T& for the return type of a function if you want to suppress copying when returning an object of type const T.

Finally, when deciding whether or not to declare a parameter to be const, one needs to bear in mind that a const argument will not match a non-const reference parameter or a non-const pointer parameter. On the other hand, if a parameter is const, it can be used for both const and non-const arguments. For example, the following will code fragment is wrong:

      void f(int& x) {}                      //non-const parameter      void g() {          const int i = 100;          f(i);                              // WRONG      } 

whereas the following version of the function f works fine for both const and non-const arguments:

      void f(const int& x) {}         // const parameter      void g() {          const int i = 100;          f(i);                              //OK          int j = 200;          f(j);                              //OK      } 

To construct a parallel example for pointer parameters, the following code fragment is wrong

      void h(int* x) {}                      // non-const parameter      void g() {          const int i = 100;          const int* p = &i;          h(p);                              // WRONG      } 

whereas the following version of the function h works fine for both const and non-const pointers:

      void h(const int* x) {}                // const parameter      void g() {          const int i = 100;          const int* p = &i;          h(p);                              // OK          int j = 200;          int* s = &j;          h(s);                              //OK      } 

[5]Of course, in some cases we do not even have the option of passing an argument by value. For example, an array cannot be passed by value in C++.




Programming With Objects[c] A Comparative Presentation of Object-Oriented Programming With C++ and Java
Programming with Objects: A Comparative Presentation of Object Oriented Programming with C++ and Java
ISBN: 0471268526
EAN: 2147483647
Year: 2005
Pages: 273
Authors: Avinash Kak

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net