Passing by reference vs Passing by address

Advertisemen

Something I realised the other day and forget to write down until now:


void swap_ref(int *a, int *b)
{
       int temp = *a;
       *a = *b;
       *b = temp;
}

void swap_add(int &a, int &b)
{
       int temp = a;
       a = b;
       b = temp;
}

These two function do exactly the same thing - swap the values of a and b. The int &a, was created by C++ to stop the unsightly pointers being used. Just keeps code cleaner.

Advertisemen

Disclaimer: Gambar, artikel ataupun video yang ada di web ini terkadang berasal dari berbagai sumber media lain. Hak Cipta sepenuhnya dipegang oleh sumber tersebut. Jika ada masalah terkait hal ini, Anda dapat menghubungi kami disini.

Tidak ada komentar:

Posting Komentar

© Copyright 2017 Game Engine Tutorial