开发者

Pass by reference vs pass by pointer? [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

When to pass by reference and when to pass by pointer in C++?

What is the difference between passing by reference and p开发者_如何转开发assing the value by a pointer?


When you pass a parameter by reference, the parameter inside the function is an alias to the variable you passed from the outside. When you pass a variable by a pointer, you take the address of the variable and pass the address into the function. The main difference is that you can pass values without an address (like a number) into a function which takes a const reference, while you can't pass address-less values into a function which takes const pointers.

Typically a C++ compiler implement a reference as a hidden pointer.

You can change your function into the pointer variant this way:

void flip(int *i) // change the parameter to a pointer type
{
    cout << "          flip start "<<"i="<< *i<<"\n"; // replace i by *i
    *i = 2*(*i); // I'm not sure it the parenthesis is really needed here,
                 // but IMHO this is better readable
    cout << "          flip exit  "<<"i="<< *i<<"\n";
}

int main()
{
    int j =1;
    cout <<"main j="<<j<<endl;
    flip(&j); // take the address of j and pass this value
    // adjust all other references ...
}


For the second part of your question, here is the code.

#include <iostream>
#include <cassert>

using namespace std;

void flip(int *i)
{
    cout << "          flip start "<<"i="<< i<<"\n";
    *i *= 2;
    cout << "          flip exit  "<<"i="<< i<<"\n";
}

int main()
{
    int j =1;
    cout <<"main j="<<j<<endl;
    flip(&j);
    cout <<"main j="<<j<<endl;
    flip(&j);
    cout <<"main j="<<j<<endl;
    flip(&j);
    cout <<"main j="<<j<<endl;

    assert(j==8);

    return 0;
}

For the first part of your question, I am new to C++ but I find it useful to pass by pointer when having to return multiple outputs for a function. Or to pass NULL as a parameter.


technically, you just need put an asterisk before the variable name to pass by reference ;) it will then now pass the address of where your variable is in your memory.

now the difference of pass by reference and pass by value is just simple. think of it this way.. imagine yourself trying to give a candy to your friend.

if you pass by value.. you: hey, i'm gonna give you something.. friend: what is it? you: here friend: thanks xD

if you pass by reference.. you: hey, i'm gonna give you something.. friend: what is it? you: it's on the right side of the table where the cookie jar is friend: thanks xD

if you pass by value, your friend doesn't know where the candy came from. it may come from the store, the fridge, or wherever. now if you pass by reference, your friend doesnt know what is it that you are gonna give him.

to relate it with programming, the candy is the value of the variable. the instruction "it's on the right side of the table where the cookie jar is" is the memory address of where the value of your variable is located. you are very much going to use this in data structures so yeah :) hope i helped you in any way xD

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜