Language/C & C++
[C++] Related to Reference
Rogue
2022. 10. 17. 09:30
반응형
// Compare Pointer with Reference
#include <iostream>
/*
void Swap(int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
}
int main(void)
{
int x = 5;
int y = 10;
Swap(x, y);
std::cout << x << " " << y << std::endl;
return 0;
}
*/
/*
void Swap(int* a, int* b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int main(void)
{
int x = 5;
int y = 10;
Swap(&x, &y);
std::cout << x << " " << y << std::endl;
return 0;
}
*/
/*
void Swap(int& ra, int& rb)
{
int temp;
temp = ra;
ra = rb;
rb = temp;
}
int main(void)
{
int x = 5;
int y = 10;
Swap(x, y);
std::cout << x << " " << y << std::endl;
return 0;
}
*/
// Compare Poiter with Reference (adding concept for return type)
#include <iostream>
int Add(int* pa)
{
(*pa)++;
return *pa;
}
int& Sub(int& rb)
{
rb--;
return rb;
}
int main(void)
{
//int a = 10;
int b = 10;
int& ref = Sub(b);
//std::cout << Add(&a) << std::endl;
std::cout << Sub(ref) << std::endl;
return 0;
}
반응형