C++
|
|
||
|
Activity:
0 comments
281 views
last activity : 07 06 2010 20:18:04 +0000
|
||
|
|
How many times have you used functions that return results through reference parameters such as this:
void func(int & ra, int & rb, int & rc) ;
You would probably call this function this way:
func(a, b, c) ;
Sometimes, if you do not want the outputs of 'ra' and 'rb' arguments, but only interested in 'rc', you might be tempted to make a call to the function as follows:
int dummy, res ;
func(dummy, dummy, res) ;
You might end up saving the space that is required for that one extra 'int' argument by using 'dummy' in place of arguments 'a' and 'b'. The function may even work properly. But, what's important to note is that if the output argument 'rc' is dependent on the values already computed in 'ra' and 'rb' by func(), then we might be looking at an entirely different scenario. Take the following example implementation of func:
void func(int & ra, int & rb, int & rc)
{
ra = 1 ;
rb = 2 ;
rc = ra + rb ;
}
void main()
{
int dummy, res ;
func(dummy, dummy, res) ;
cout << "Value is: " << res ;
}
What do you think is the output? It's not the value '3', but it's '4'. Disaster!! You would have successfully traded accuracy / correctness for a simple saving of 4 bytes of virtual space.
Hence, in cases where you are not sure if the values of one result parameter affects other results, safely use different variables to hold output parameters.

|
|
|
|
|
|
|
|
|
|
|
|
What is the best C++ compiler available? |
How many times have you used functions that return results through reference parameters such as this: void func(int ra, int rb, int rc) ; You would probably call this function this way: func(a, b, c) ; Sometimes, if you do not want the outputs... |
Has C++ been the positive step forward from C? |