Reference Variables.


Reference variables allow two variable names to address the same memory location. The following example shows the technique in its simplest form.

        
    #include 
    
    main()
    {
        int var1;
        int &var2 = var1;       // var2 is a reference variable.

        var1 = 10;
    
        cout << "var1 = " << var1 << endl;
        cout << "var2 = " << var2 << endl;
    }

Generally, you would not use a reference variable in this way. Its more likely that they would be used in function parameter lists to make the passing of pointers more readable.

This gives C++ the ability to provide a different approch to changing a variable from within a function. Consider the two following programs.


    #include <stdio.h>

    void Square(int *pVal);

    main()
    {
        int Number=10;

        printf("Number is %d\n", Number);

        Square(&Number);

        printf("Number is %d\n", Number);
    }

    void Square(int *pVal)
    {
        *pVal *= *pVal;            
        
        printf("Number is %d\n", *pVal);
    }


    #include <stdio.h>

    void Square(int &Val);

    main()
    {
        int Number=10;

        printf("Number is %d\n", Number);

        Square(Number);

        printf("Number is %d\n", Number);
    }

    void Square(int &Val)
    {
        Val *= Val;
        
        printf("Number is %d\n", Val);
    }

The program on the right should be clearer to read because you do not need to worry about pointer dereferencing.


Examples:

o Example program.

o Example C program.


See Also:


C References

o data types.


Top Master Index Keywords Functions


Martin Leslie 29-Sep-96

alue='30'>30space:mode:
authorTobias Klauser <tklauser@distanz.ch>2016-10-20 15:44:19 +0200
committerTobias Klauser <tklauser@distanz.ch>2017-02-15 10:34:18 +0100
commit5db4992d8f040b8d8db0b86d42806e0c417f7ccf (patch)
tree5b06e952af482d45f3ade64e77824662e34b7fa2
parent370ebb0ef6255132373ed35d13e7b1d8d2eb7003 (diff)

usbnet: pegasus: Use net_device_stats from struct net_devicends-private-remove
Instead of using a private copy of struct net_device_stats in struct pegasus, use stats from struct net_device. Also remove the now unnecessary .ndo_get_stats function. Cc: Petko Manolov <petkan@nucleusys.com> Cc: linux-usb@vger.kernel.org Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
Diffstat