开发者

Pass parameter by reference vs use Instance variable

I started to read a book about C++ and found the following code. It is an example on how you can send pass parameters by reference.

#include <iostream>

void swap(int &x, int &y);

int main()
{
    int x = 5, y = 10;

    std::cout << "Main. Before swap, x: " << x
              << " y: " << y << "\n";
    swap(x, y);
    std::cout << "Main. After swap, x: " << x
              << " y: " << y << "\n";
    return 0;
}

void swap(int &rx, int &am开发者_如何学运维p;ry)
{
    int temp;

    std::cout << "Swap. Before swap, rx: " << rx
              << " ry: " << ry << "\n";

    temp = rx;
    rx = ry;
    ry = temp;

    std::cout << "Swap. After swap, rx: " << rx
              << " ry: " << ry << "\n";
}

.

Main. Before swap, x:5 y: 10
Swap. Before swap, rx:5 ry:10
Swap. After swap, rx:10 ry:5
Main. After swap, x:10, y:5

The logic is clear to me. Now this may be a very stupid question (I'm not very experienced yet), but why can't you just declare private: int x as an instance variable? Isn't x in this case directly accessible everywhere in your class? (without the need for specifying parameters at all)? Thanks in advance for your answers!


For several reasons.

  1. You should declare variables at the narrowest scope possible. Why? Look at 2 & 3
  2. Variables are expensive, they take up memory, you only want them around as long as you need them.
  3. The greater a variables scope (i.e. how much code the variable is visible to) the greater the chance that you will mistakenly use the variable, and therefore it's value may change unexpectedly. This will be a bug, good luck hunting that one down.
  4. Tight coupling (this is bad). If you write a class and put a swap method on the class, and you write it so it uses instance member x (not method variable x), then that swap method CAN ONLY EVER swap using x, if in time you need it to swap on a different variable (or the parameter of another method on the class) then you've to move the value into x which is Inefficient & goto 5. Isn't it better to call the swap function with the values you have to hand, without needing to know there's a special x variable that you have to set first?
  5. Error prone. Will this second method be called while another method is using the swap method? What should the value of x be after it's called? You're introducing lots of context around swap and knowing when it's ok to call swap, and what can call swap. This is bad, the more self contained any piece of code is, then the less of have to worry about it, and about how it's used.
  6. No other class can re-use your swap method, every class that needs a swap method must implement it's own, and this is a huge big no-no for more reasons than I can count here, but can sum up as it voliates the DRY Principal

All of these problems can be removed by simply passing the values by reference. Bit of a no-brainer really :)

Hope this helps.


Passing values via arguments to a function ensures modularity in your code. It sounds like you're just starting out with C++, so I'm not sure how familiar you are with object oriented programming. Functions/methods represent a layer of encapsulation. Your swap() function should encapsulate the logic needed to perform its function/purpose. The caller should not be concerned with how this is accomplished. If your swap() function must assert there is a global variable available in the program, then it's not fully encapsulating the logic of "swapping".

Also, Lets say you wanted to reuse this function elsewhere in your class. It would be difficult and clumsy to use a set of global variables for calling this function. In addition, you may have other locations in your class that are referencing those global variables, and therefore your other calls to swap() would change those values, potentially causing confusion in other areas of the code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜