-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c60e529
commit db0db2d
Showing
10 changed files
with
227 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
**/output/** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
# examples | ||
# Examples | ||
|
||
기술 블로그 작성 등에 쓰인 예시 코드들 모음 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#include <iostream> | ||
using namespace std; | ||
|
||
int main(void) | ||
{ | ||
int num=100; | ||
int &ref=num; | ||
|
||
cout<<"num: "<<&num<<endl; | ||
cout<<"ref: "<<&ref<<endl; | ||
// num: 0x16b3dec38 | ||
// ref: 0x16b3dec38 | ||
|
||
num=200; | ||
cout<<"val: "<<num<<endl; | ||
cout<<"ref: "<<ref<<endl; | ||
// val: 200 | ||
// ref: 200 | ||
|
||
ref=300; | ||
cout<<"val: "<<num<<endl; | ||
cout<<"ref: "<<ref<<endl; | ||
// val: 300 | ||
// ref: 300 | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#include <iostream> | ||
using namespace std; | ||
|
||
int main(void) | ||
{ | ||
int *ptr=new int; | ||
int &ref=*ptr; | ||
ref=20; | ||
cout<<"ref: "<<ref<<endl; | ||
cout<<"*ptr: "<<*ptr<<endl; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#include <iostream> | ||
using namespace std; | ||
|
||
int main(void) | ||
{ | ||
int num1=100; | ||
int &num2=num1; | ||
int &num3=num1; | ||
int &num4=num3; | ||
|
||
cout<<"num1: "<<num1<<endl; | ||
cout<<"num2: "<<num2<<endl; | ||
cout<<"num3: "<<num3<<endl; | ||
cout<<"num4: "<<num4<<endl; | ||
// num1: 100 | ||
// num2: 100 | ||
// num3: 100 | ||
// num4: 100 | ||
|
||
// int &ref; | ||
// int &ref=NULL; | ||
|
||
int arr[3]={1, 3, 5}; | ||
int &first=arr[0]; | ||
int &second=arr[1]; | ||
int &third=arr[2]; | ||
|
||
cout<<"first: "<<first<<endl; | ||
cout<<"second: "<<second<<endl; | ||
cout<<"third: "<<third<<endl; | ||
// first: 1 | ||
// second: 3 | ||
// third: 5 | ||
|
||
int num=10; | ||
int *ptr=# | ||
int *(&ptrRef)=ptr; | ||
|
||
cout<<"ptr: "<<*ptr<<endl; | ||
cout<<"ptrRef: "<<*ptrRef<<endl; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include <iostream> | ||
using namespace std; | ||
|
||
int main(void) | ||
{ | ||
// int &ref=20; // 컴파일 실패 | ||
const int &ref=20; | ||
|
||
const int num=100; | ||
const int &ref1=num; | ||
const int &ref2=100; | ||
|
||
// const int num=100; | ||
// const int &ref=num; | ||
|
||
// const int num=100; | ||
// int &ref=num; // 컴파일 실패 | ||
// ref+=10; | ||
// cout<<"num1: "<<num<<endl; | ||
|
||
// int &num3=num1; | ||
// int &num4=num3; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#include <iostream> | ||
using namespace std; | ||
|
||
// int SwapByVal(int num1, int num2) | ||
// { | ||
// int temp=num1; | ||
// num1=num2; | ||
// num2=temp; | ||
// } | ||
|
||
// int SwapByPtr(int * ptr1, int * ptr2) | ||
// { | ||
// int temp=*ptr1; | ||
// *ptr1=*ptr2; | ||
// *ptr2=temp; | ||
// } | ||
|
||
int SwapByRef(int &ref1, int &ref2) | ||
{ | ||
int temp=ref1; | ||
ref1=ref2; | ||
ref2=temp; | ||
} | ||
|
||
int main(void) | ||
{ | ||
int val1=10; | ||
int val2=20; | ||
SwapByRef(&val1, &val2); | ||
cout<<"val1: "<<val1<<endl; | ||
cout<<"val2: "<<val2<<endl; | ||
// val1: 20 | ||
// val2: 10 | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#include <iostream> | ||
using namespace std; | ||
|
||
int Do(int &ref) | ||
{ | ||
ref++; | ||
} | ||
|
||
int Func(const int &ref) | ||
{ | ||
// ref=1; // 컴파일 에러 | ||
} | ||
|
||
int main(void) | ||
{ | ||
int num=1; | ||
Do(num); | ||
cout<<num<<endl; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#include <iostream> | ||
using namespace std; | ||
|
||
int& RefReturnFunc(int &ref) | ||
{ | ||
ref++; | ||
return ref; | ||
} | ||
|
||
int Func(int &ref) | ||
{ | ||
ref++; | ||
return ref; | ||
} | ||
|
||
int main(void) | ||
{ | ||
// int num1=1; | ||
// int num2=RefReturnFunc(num1); | ||
|
||
// num1+=10; | ||
// num2+=100; | ||
// cout<<"num1: "<<num1<<endl; | ||
// cout<<"num2: "<<num2<<endl; | ||
// // num1: 12 | ||
// // num2: 102 | ||
// cout<<"num1: "<<&num1<<endl; | ||
// cout<<"num2: "<<&num2<<endl; | ||
// // num1: 0x16f9f2c38 | ||
// // num2: 0x16f9f2c34 | ||
|
||
|
||
int num1=1; | ||
int num2=RefReturnFunc(num1); | ||
int &num3=RefReturnFunc(num1); | ||
int num4=Func(num1); | ||
// int &num5=Func(num1); // 컴파일 에러 | ||
|
||
cout<<"num1: "<<&num1<<endl; | ||
cout<<"num2: "<<&num2<<endl; | ||
cout<<"num3: "<<&num3<<endl; | ||
cout<<"num4: "<<&num4<<endl; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#include <iostream> | ||
using namespace std; | ||
|
||
int& RefReturnFunc() | ||
{ | ||
int num=20; | ||
num+=10; | ||
return num; | ||
} | ||
|
||
int main(void) | ||
{ | ||
int num=RefReturnFunc(); | ||
cout<<"num: "<<endl; | ||
cout<<"num: "<<endl; | ||
cout<<"num: "<<endl; | ||
cout<<"num: "<<num<<endl; | ||
return 0; | ||
} |