728x90
반응형
// 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;
}
728x90
반응형

'Language > C & C++' 카테고리의 다른 글

[C++] Related to Namespace  (0) 2022.10.17
[C++] Related to Inline  (0) 2022.10.17
[C] Related to Preprocessor  (0) 2022.10.17
Exception handling  (0) 2022.10.14
Implement reference-based call-by-reference  (0) 2022.09.28

+ Recent posts