Skip to content

Commit

Permalink
feat(c++): reference syntax example
Browse files Browse the repository at this point in the history
  • Loading branch information
bugoverdose committed Jun 1, 2024
1 parent c60e529 commit db0db2d
Show file tree
Hide file tree
Showing 10 changed files with 227 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/output/**
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# examples
# Examples

기술 블로그 작성 등에 쓰인 예시 코드들 모음
26 changes: 26 additions & 0 deletions c++/cpp-reference-syntax/ex1.cpp
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;
}
12 changes: 12 additions & 0 deletions c++/cpp-reference-syntax/ex2.cpp
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;
}
43 changes: 43 additions & 0 deletions c++/cpp-reference-syntax/ex3.cpp
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=&num;
int *(&ptrRef)=ptr;

cout<<"ptr: "<<*ptr<<endl;
cout<<"ptrRef: "<<*ptrRef<<endl;

return 0;
}
24 changes: 24 additions & 0 deletions c++/cpp-reference-syntax/ex4.cpp
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;
}
35 changes: 35 additions & 0 deletions c++/cpp-reference-syntax/ex5.cpp
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;
}
20 changes: 20 additions & 0 deletions c++/cpp-reference-syntax/ex6.cpp
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;
}
44 changes: 44 additions & 0 deletions c++/cpp-reference-syntax/ex7.cpp
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;
}
19 changes: 19 additions & 0 deletions c++/cpp-reference-syntax/ex8.cpp
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;
}

0 comments on commit db0db2d

Please sign in to comment.