How *val and &val differ when passed by reference in C++

void funct( int *val1, int &val2)
{
    *val1= 20;
    val2= 22;
}

int main()
{
    int v1=10, v2=12;
    funct(&v1, v2);
    cout<<"v1: "<<v1<<endl<<"v2: "<<v2<<endl;
    return 1;
}

These both, v1 and v2 are called by reference by funct(). But I don't understand, what is difference between both ways apart from syntactical difference. Few questions I want to ask are,

  • Are both totally same thing?
  • Are both equally good in terms of coding practice?
  • Situations where one will work, other don't?
  • What are other things that I can do using one, that I can't do using other?
  • To simplify, I would call:

    int *val1 ------- Method 1
    int &val2 ------- Method 2
    

    I guess you are trying to understand the functioning of call by references and pointers. For more and better understanding I recommend you to read answers from this question.

    Are there benefits of passing by pointer over passing by reference in C++?

    Good Luck!


    In terms of the functionality of the code that you've provided, there's no difference between these two functions:

    void swap_with_addresses(int * a, int * b) {
        int tmp = *a;
        *a = *b;
        *b = tmp;
    }
    
    void swap_with_references(int & a, int & b) {
        int tmp = a;
        a = b;
        b = tmp;
    }
    
    int main() {
        int a = 5, b = 10;
        std::cout << a << "," << b << std::endl;
        swap_with_addresses(&a, &b);
        std::cout << a << "," << b << std::endl;
        swap_with_references(a, b);
        std::cout << a << "," << b << std::endl;
        return 0;
    }
    

    Executing this code yields:

    5,10
    10,5
    5,10
    

    Getting to your questions:

  • Are both totally same thing? Yes and no. You might get the same resulting machine code, but you use them in the function differently as you see in your own code. Because you cannot change those pointers and you really don't want to, a reference makes more sense.

  • Are both equally good in terms of coding practice? In C you have no option but in C++ you should use references. If you pass arrays you might think about though there exist C++ arrays (std::arrays) which can be referenced as well. You still can use pointers for return-by-reference method because C++ knows C mostly.

  • Situations where one will work, other don't? For C arrays a constant pointer might help, but you have data structures by STL (vector and array) which can do the same and with references.

  • What are other things that I can do using one, that I can't do using other? Well, pointers are more powerful than references but I think the additional power you get compared does not really help you if you want to pass values by reference. Also remember you can still return one value which also could be composed by struct or class. If you prefer to return values, only one is possible (unlike in Matlab for example), but you can return them as bundles through other datatype definitions.

  • NOTE: Read the provided link by @hardydragon with an awesome explanation

    About References in C++:

    Though the question is explained in numerous books about C++, let me try to focus in your specific question.

    In C there only exists the pointer. You can pass them as being the "reference". When C++ was introduced references came into place where you can define

    int &val2
    

    But here is the catch, the reference usually does the same thing as the pointer, but you cannot change address, because there is no direct address-access to the variable. If you access a C++ reference it will give you the value you are pointing/referencing and it will allow you to modify that value; they work like normal variables. On the downside in its definition you have to tell the reference where it references immediately:

    int a = 5; int &b = a; // If you pass by reference it has the same effect for the given parameter to your function

    Pointers can point at runtime to null, to something invalid or to whatever you want within the memory. You can change the address at any part. Also you can do pointer arithmetic which is not possible with references. Of course that is dangerous, but you might already know why.

    The problem about pointers is/was, that people get/got confused with them, especially the dereference (&) operator and pointer (*) definition is/was not clear for many. Considering pointer-to-pointers does not really improve the situation. So using references like it is in C++ made the live easier for many people. This does not mean that you should not understand pointers. The point is your code becomes more readable.

    So as general hint because you use C++, use references. It is not always possible to use them due it's limitations, but wherever possible use them.

    So this is my opinion but for call by reference you should use references, unless there are reasons you can't.

    链接地址: http://www.djcxy.com/p/20738.html

    上一篇: 如何在另一个类的方法中使用一个类的实例?

    下一篇: 当通过C ++中的引用传递时,* val和&val的区别如何?